1

我有一个非常简单的 Spring 3 ReST Web 服务,看起来像这样

@RequestMapping(method = RequestMethod.POST, value = "/user")
public @ResponseBody
UserDTO saveUser(@RequestBody UserDTO userDTO)
{
    // Functionality removed as it is not needed for this example
    return userDTO;
}

这个网络服务很好用。不过,当我添加一个“包罗万象”的@ExceptionHandler 时,我注意到了一个奇怪的问题。在这个 POST 调用中,即使它正确返回,似乎在某处的幕后抛出了异常。因此,如果我添加代码:

@ExceptionHandler(Exception.class)
public ModelAndView handleRemainingExceptions(Exception ex)
{
    logger.error("Unhandled exception", ex);
    return new ModelAndView("error");
}

我突然收到这个错误:

07.06.2011 22:54:05 ERROR-ImportController: [Unhandled exception]:
Cannot extract parameter (UserDTO userDTO): no Content-Type found]
org.springframework.web.HttpMediaTypeNotSupportedException: Cannot extract parameter (UserDTO userDTO): no Content-Type found
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:620)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:346)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

有人知道这里发生了什么吗?

4

1 回答 1

-1

该异常似乎是在抱怨您的请求不包含 Content-Type 标头,因此 Spring 不知道使用哪个 MessageConverter 来创建方法参数 UserDTO。

于 2011-07-05T16:28:28.903 回答