Pergunta

i am starting an embedded Tomcat 8 and bootstrap the spring application context using an implementation of the WebApplicationInitializer interface. This implementation also add's a DispatcherServlet to the Tomcat Servlet Context, my code:

@Override
public void onStartup(final ServletContext servletContext) throws ServletException
{
final XmlWebApplicationContext dispatcherContext = new XmlWebApplicationContext();
dispatcherContext.setConfigLocation("/path/to/context/defintion.xml");

// Register and map the dispatcher servlet
final ServletRegistration.Dynamic dispatcher =
servletContext.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("/test/*");
}

ass seen in the spring documentation.

somewhere i do:

Tomcat t = new Tomcat()
// ... some config ...
t.start()

which than triggers the method seen above. Works fine.

The context defintion looks like the following:

<!-- annotation config -->
<context:annotation-config/>

<!-- scan for controllers -->
<context:component-scan base-package="com.samples.controller"/>

<!-- static resources -->
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/js/**" location="/js/"/>

My Controller:

@Controller
// @Order(value = 1)
public class ReadyController
{

@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView ready(final HttpServletRequest request, final HttpServletResponse
    response)
{
  try
  {
    response.getWriter().print("Ready!");
  }
  catch (IOException e)
  {
    e.printStackTrace();
  }
  return null;
}

}

When i try to open the ready method in my browser it says 404 Not Found. When i remove the mvc:resources mappings than suddenly it works. I tryed to add an Order annotation to the controller and also an order attribute to the mvc:resources mapping but it didnt work.

Anyone has seen a solution to this?

i used: Java8, Embeddded Tomcat8, Spring4


EDIT: added logfile output

Here the log output with added mvc:resources tag in context:

2014-04-29 15:41:31 DEBUG DispatcherServlet:838 - DispatcherServlet with name 'dispatcher' processing GET request for [/test]
2014-04-29 15:41:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@3cf4de47] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE BeanNameUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:41:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@5f1fb0ba] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE SimpleUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:41:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.SimpleUrlHandlerMapping@29f2754] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE SimpleUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:41:31 WARN  PageNotFound:1110 - No mapping found for HTTP request with URI [/test] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:41:31 TRACE DispatcherServlet:1053 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@2bc277d6
2014-04-29 15:41:31 DEBUG DispatcherServlet:991 - Successfully completed request
2014-04-29 15:41:31 TRACE XmlWebApplicationContext:331 - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-04-29 15:41:31 TRACE XmlWebApplicationContext:331 - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-04-29 15:41:31 TRACE ClassPathXmlApplicationContext:331 - Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@3c130745: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]
2014-04-29 15:41:31 TRACE StaticApplicationContext:331 - Publishing event in org.springframework.context.support.StaticApplicationContext@63440df3: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[1ms]; status=[OK]

if i remove the mvc:resources tags from teh context file the reuqest works and the log output looks like this:

2014-04-29 15:42:31 TRACE DispatcherServlet:1043 - Bound request context to thread: org.apache.catalina.connector.RequestFacade@1f28ea73
2014-04-29 15:42:31 DEBUG DispatcherServlet:838 - DispatcherServlet with name 'dispatcher' processing GET request for [/test]
2014-04-29 15:42:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@1820ce89] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:42:31 TRACE BeanNameUrlHandlerMapping:127 - No handler mapping found for [/test]
2014-04-29 15:42:31 TRACE DispatcherServlet:1091 - Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@7753d076] in DispatcherServlet with name 'dispatcher'
2014-04-29 15:42:31 DEBUG DefaultAnnotationHandlerMapping:124 - Mapping [/test] to HandlerExecutionChain with handler [com.samples.controller.ReadyController@5ce05166] and 1 interceptor
2014-04-29 15:42:31 TRACE DispatcherServlet:1131 - Testing handler adapter [org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter@303e6671]
2014-04-29 15:42:31 TRACE DispatcherServlet:1131 - Testing handler adapter [org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter@75da797a]
2014-04-29 15:42:31 TRACE DispatcherServlet:1131 - Testing handler adapter [org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter@384cb80]
2014-04-29 15:42:31 DEBUG DispatcherServlet:925 - Last-Modified value for [/test] is: -1
2014-04-29 15:42:31 DEBUG HandlerMethodInvoker:172 - Invoking request handler method: public void com.samples.controller.ReadyController.ready(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2014-04-29 15:42:31 DEBUG DispatcherServlet:1012 - Null ModelAndView returned to DispatcherServlet with name 'dispatcher': assuming HandlerAdapter completed request handling
2014-04-29 15:42:31 TRACE DispatcherServlet:1053 - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@1f28ea73
2014-04-29 15:42:31 DEBUG DispatcherServlet:991 - Successfully completed request
2014-04-29 15:42:31 TRACE XmlWebApplicationContext:331 - Publishing event in WebApplicationContext for namespace 'dispatcher-servlet': ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
2014-04-29 15:42:31 TRACE XmlWebApplicationContext:331 - Publishing event in Root WebApplicationContext: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
2014-04-29 15:42:31 TRACE ClassPathXmlApplicationContext:331 - Publishing event in org.springframework.context.support.ClassPathXmlApplicationContext@353d0772: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]
2014-04-29 15:42:31 TRACE StaticApplicationContext:331 - Publishing event in org.springframework.context.support.StaticApplicationContext@7113b13f: ServletRequestHandledEvent: url=[/test]; client=[0:0:0:0:0:0:0:1]; method=[GET]; servlet=[dispatcher]; session=[null]; user=[null]; time=[2ms]; status=[OK]

EDIT: if i remove the mvc:resources tag and instead use the mvc:default-servlet-handler tag i get the same result, request not working.

Foi útil?

Solução

stackoverflow.com/q/7910845/2231632 - i believe this may be of some help.

Copying the answer here: Try adding <mvc:annotation-driven /> to your context. overrides the default behavior of spring mvc. If you add <mvc:annotation-driven /> to your spring-servlet.xml, it should forcibly register all required handlers

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top