I am setup a sample Code base using Spring MVC in Eclipse and JBoss 6.2.
But I get '404' with http://localhost:8080/rest/simple/main
Jboss log as below:
2015-07-29 11:51:27,356 ERROR [controller.simpleController] (http-/0.0.0.0:8080-1) get request
2015-07-29 11:51:27,391 WARN [org.springframework.web.servlet.PageNotFound] (http-/0.0.0.0:8080-1) No mapping found for HTTP request with URI [/rest/WEB-INF/views/main.jsp] in DispatcherServlet with name 'dispatcher'
Directory :
>rest-server-simple
> -src
> -main
-java
-config
-InitConfig.java
-ServletConfig.java
-controller
-simpleController.java
> -webapp
> -WEB-INF
-jboss-web.xml
>views
-main.jsp
InitConfig:
public class InitConfig implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ServletConfig.class);
ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx));
registration.setLoadOnStartup(1);
registration.addMapping("/*"); }}
ServletConfig:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages ="controller")
public class ServletConfig {
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
jboss-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-web>
<context-root>/rest</context-root>
</jboss-web>
simpleController:
@Controller
@RequestMapping(value = "/simple")
public class simpleController {
private static final Logger logger = LoggerFactory.getLogger(simpleController.class);
@RequestMapping(value = "/main", method = RequestMethod.GET)
public String hello(){
logger.error("get request");
return "main";
}
}
registration.addMapping("/*");
Change it to
registration.addMapping("/");
There is a difference between /*
and /
.
/*
indicates that every request will be handled by DispatcherServlet
, in this case retrieval of a jsp
or anything like .../abc.xyz
etc will also be forwarded to Dispatcher
, so when controller requests for a view
it actually looks for RequestMapping
mapped for /WEB-INF/views/main.jsp
but
/
tells container that only those requests that do not have pathinfo i.e /rest/simple/main
will be handled by DispatcherServlet
.
Hmm.. What I have found that jboss AS 7
doesn't like overriding default servlet
i.e. /
without web.xml
and hence you are still getting 404
and not even getting anything on the logger, Reason being simple is that Dispatcher
is never mapped to any url
. If you want to check that just add following after addMapping("/*");
System.out.println("registration.getMappings() = " + registration.getMappings());
It works fine with Tomcat >= 7.0.15
or WildFly
have checked on both.
To make it work on JBoss7
there are few options:
1. Change DispatcherServlet
mapping from /
to *.htm
or something except DefaultServlet Mapping
.
2. Switch your Configuration to web.xml
. You will have to initialize DispatcherServlet
there and pass Annotated class as `contextConfigLocation. Check here for REF