Spring MVC Java config -
i want set simple response body spring webapp. problem simple given web error.
my pom.xml is:
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelversion>4.0.0</modelversion> <groupid>pt.dummy</groupid> <artifactid>dummy</artifactid> <packaging>war</packaging> <version>0.0.1-snapshot</version> <name>dummy maven webapp</name> <url>http://maven.apache.org</url> <properties> <spring-framework.version>3.2.3.release</spring-framework.version> <tomcat.servlet.version>7.0.42</tomcat.servlet.version> <log4j.version>1.7.5</log4j.version> <java.version>1.7</java.version> <junit.version>4.11</junit.version> </properties> <dependencies> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-core</artifactid> <version>${tomcat.servlet.version}</version> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-logging-juli</artifactid> <version>${tomcat.servlet.version}</version> </dependency> <dependency> <groupid>org.apache.tomcat.embed</groupid> <artifactid>tomcat-embed-jasper</artifactid> <version>${tomcat.servlet.version}</version> <exclusions> <exclusion> <groupid>org.eclipse.jdt.core.compiler</groupid> <artifactid>ecj</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-core</artifactid> <version>${spring-framework.version}</version> <scope>compile</scope> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>${spring-framework.version}</version> <scope>compile</scope> </dependency> <dependency> <groupid>javax.servlet</groupid> <artifactid>javax.servlet-api</artifactid> <version>3.0.1</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalname>dummy</finalname> <testresources> <testresource> <!-- declared explicitly spring config files can placed next corresponding junit test class (see example validatortests) --> <directory>${project.basedir}/src/test/java</directory> </testresource> <testresource> <directory>${project.basedir}/src/test/resources</directory> </testresource> </testresources> <plugins> <plugin> <groupid>org.apache.maven.plugins</groupid> <artifactid>maven-compiler-plugin</artifactid> <version>3.1</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>
my web initializer is:
package dummy.web.config; import javax.servlet.filter; import org.springframework.web.filter.characterencodingfilter; import org.springframework.web.servlet.support.abstractannotationconfigdispatcherservletinitializer; public class webappinitializer extends abstractannotationconfigdispatcherservletinitializer { @override protected class<?>[] getrootconfigclasses() { return new class<?>[] { coreconfig.class }; } @override protected class<?>[] getservletconfigclasses() { return new class<?>[] { webconfig.class }; } @override protected string[] getservletmappings() { return new string[] { "/" }; } @override protected filter[] getservletfilters() { characterencodingfilter characterencodingfilter = new characterencodingfilter(); characterencodingfilter.setencoding("utf-8"); return new filter[] { characterencodingfilter}; } }
core config:
package dummy.web.config; import org.springframework.context.annotation.configuration; @configuration public class coreconfig { }
web config:
package dummy.web.config; import org.springframework.context.annotation.componentscan; import org.springframework.context.annotation.configuration; import org.springframework.web.servlet.config.annotation.enablewebmvc; @configuration @enablewebmvc @componentscan(basepackages = {"dummy.web.controller"}) public class webconfig { }
site controller:
package dummy.web.controller; //import org.slf4j.logger; //import org.slf4j.loggerfactory; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.responsebody; @controller @requestmapping("/") public class sitecontroller { // private static final logger log = loggerfactory.getlogger(sitecontroller.class); @requestmapping(method = requestmethod.get) @responsebody public string gethome() { // log.debug("home responsebody"); return "response body"; } }
could indicate changes necessary add make work?
i believe because of tomcat. tutorials on web put spring mvc servlet directly in application context. never worked me.
on tomcat7 (even xml configuration) have create 2 contexts: 1 overall app , 1 spring web mvc. has with
@requestmapping("/")
the server assigns "/" mapping default built-in servlet. want him (or or her) do. need spring mvc map "/".
maybe (the architects) thought springmvc specific servlet , shouldn't map root contex. instead, should under own mapping (eg "/springmvc/"). , expects have real dispatcher which...dispatches between springmvc , whatever other servlets.
for magical reason, in tomcat 7.0.29 wasn't able "dispatch" if tried hijack "/". on recent version, mapping "/" works .but need separate web mvc context/root context.
i not use abstractannotationconfigdispatcherservletinitializer have translate code below. adapted tutorial on migrating xml javaconfig spring-app-migration-from-xml-to-java-based-config
public class webinit implements webapplicationinitializer { private static final string dispatcher_servlet_name = "spring-mvc"; private static final string dispatcher_servlet_mapping = "/"; @override public void onstartup(servletcontext servletcontext) throws servletexception { //if want use xml configuration, comment following 2 lines out. annotationconfigwebapplicationcontext appcontext = new annotationconfigwebapplicationcontext(); appcontext.register(coreconfig.class); appcontext.setdisplayname("removed customer name"); //if want use xml configuration, uncomment following lines. //xmlwebapplicationcontext rootcontext = new xmlwebapplicationcontext(); //rootcontext.setconfiglocation("classpath:mvc-servlet.xml"); annotationconfigwebapplicationcontext mvccontext = new annotationconfigwebapplicationcontext(); mvccontext.register(servletconfig.class); servletregistration.dynamic springmvc = servletcontext.addservlet(dispatcher_servlet_name, new dispatcherservlet(mvccontext)); springmvc.setloadonstartup(1); springmvc.addmapping(dispatcher_servlet_mapping); enumset<dispatchertype> dispatchertypes = enumset.of(dispatchertype.request, dispatchertype.forward); characterencodingfilter characterencodingfilter = new characterencodingfilter(); characterencodingfilter.setencoding("utf-8"); characterencodingfilter.setforceencoding(true); filterregistration.dynamic characterencoding = servletcontext.addfilter("characterencoding", characterencodingfilter); characterencoding.addmappingforurlpatterns(dispatchertypes, true, "/*"); filterregistration.dynamic security = servletcontext.addfilter("springsecurityfilterchain", new delegatingfilterproxy()); security.addmappingforurlpatterns(dispatchertypes, true, "/*"); servletcontext.addlistener(new contextloaderlistener(appcontext)); }
Comments
Post a Comment