415将json发送到弹簧控制器时,不支持的介质类型

时间:2015-05-14 10:59:39

标签: java json spring jackson type-conversion

我发送带有“Content-Type”的POST http请求 "application/json"到我的控制器

我有以下依赖项:

     <!--JSON-->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
    </dependency>

以下控制方法:

    @RequestMapping(value = "sendData.json", method = RequestMethod.POST, consumes = "application/json")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void putJsonData(@RequestBody RequestResponseWrapper requestResponseWrapper) {
        ...
    }

RequestResponseWrapper并且所有类都有默认构造函数

我看到以下春季日志:

4751 [qtp333509504-16] TRACE o.s.web.servlet.DispatcherServlet - Bound request context to thread: (POST //localhost:8087/MyCompanyMocks/mvc/***/sendData.json)@883530709 org.eclipse.jetty.server.Request@34a99bd5 
4751 [qtp333509504-16] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'Spring MVC Dispatcher Servlet' processing POST request for [/MyCompanyMocks/mvc/***/sendData.json] 
4753 [qtp333509504-16] TRACE o.s.web.servlet.DispatcherServlet - Testing handler map [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping@536d6f00] in DispatcherServlet with name 'Spring MVC Dispatcher Servlet' 
4754 [qtp333509504-16] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /***/sendData.json 
4755 [qtp333509504-16] TRACE o.s.w.s.m.m.a.RequestMappingHandlerMapping - Found 1 matching mapping(s) for [/***/sendData.json] : [{[/***/sendData.json],methods=[POST],params=[],headers=[],consumes=[application/json],produces=[],custom=[]}] 
4756 [qtp333509504-16] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public void com.MyCompany.mocks.***.controller.***Controller.putJsonData(com.MyCompany.mocks.***.dto.***.RequestResponseWrapper)] 
4756 [qtp333509504-16] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean '***Controller' 
4756 [qtp333509504-16] TRACE o.s.web.servlet.DispatcherServlet - Testing handler adapter [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter@1f878a58] 
7759 [qtp333509504-16] TRACE o.s.w.m.annotation.ModelFactory - Selected @ModelAttribute method public com.MyCompany.mocks.***.dto.***.RequestResponseWrapper com.MyCompany.mocks.***.controller.***Controller.createRequestResponseWrapper() 
8485 [qtp333509504-16] TRACE o.s.w.m.s.InvocableHandlerMethod - Invoking [***Controller.createRequestResponseWrapper] method with arguments [] 
8485 [qtp333509504-16] TRACE o.s.w.m.s.InvocableHandlerMethod - Method [createRequestResponseWrapper] returned [com.MyCompany.mocks.***.dto.***.RequestResponseWrapper@2f033262] 
9339 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMethodArgumentResolver@2d121a3f] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9340 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.method.annotation.RequestParamMapMethodArgumentResolver@24ecc563] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9340 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMethodArgumentResolver@20b32d70] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9340 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.PathVariableMapMethodArgumentResolver@2448f6ff] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9340 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMethodArgumentResolver@1da997ab] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9341 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.MatrixVariableMapMethodArgumentResolver@12cb0241] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9341 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor@430ecb7c] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9341 [qtp333509504-16] TRACE o.s.w.m.s.HandlerMethodArgumentResolverComposite - Testing if argument resolver [org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor@3841236] supports [class com.MyCompany.mocks.***.dto.***.RequestResponseWrapper] 
9652 [qtp333509504-16] TRACE o.s.w.s.m.m.a.ServletInvocableHandlerMethod - Error resolving argument [0] [type=com.MyCompany.mocks.***.dto.***.RequestResponseWrapper]
HandlerMethod details: 
Controller [com.MyCompany.mocks.***.controller.***Controller]
Method [public void com.MyCompany.mocks.***.controller.***Controller.putJsonData(com.MyCompany.mocks.***.dto.***.RequestResponseWrapper)]

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:158) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:149) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:100) ~[spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) [spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) [javax.servlet-api-3.1.0.jar:3.1.0]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) [spring-webmvc-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [javax.servlet-api-3.1.0.jar:3.1.0]
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:806) [jetty-servlet-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587) [jetty-servlet-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:550) [jetty-security-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1128) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515) [jetty-servlet-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1062) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:113) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.Server.handle(Server.java:507) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:284) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:237) [jetty-server-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.io.AbstraMyCompanyonnection$ReadCallback.succeeded(AbstraMyCompanyonnection.java:240) [jetty-io-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:93) [jetty-io-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.io.SeleMyCompanyhannelEndPoint$2.run(SeleMyCompanyhannelEndPoint.java:53) [jetty-io-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceRun.produceAndRun(ExecuteProduceRun.java:191) [jetty-util-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.util.thread.strategy.ExecuteProduceRun.run(ExecuteProduceRun.java:126) [jetty-util-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:641) [jetty-util-9.3.0.M2.jar:9.3.0.M2]
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:559) [jetty-util-9.3.0.M2.jar:9.3.0.M2]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
9756 [qtp333509504-16] DEBUG o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolving exception from handler [public void com.MyCompany.mocks.***.controller.***Controller.putJsonData(com.MyCompany.mocks.***.dto.***.RequestResponseWrapper)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported 
9758 [qtp333509504-16] DEBUG o.s.w.s.m.a.ResponseStatusExceptionResolver - Resolving exception from handler [public void com.MyCompany.mocks.***.controller.***Controller.putJsonData(com.MyCompany.mocks.***.dto.***.RequestResponseWrapper)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported 
9758 [qtp333509504-16] DEBUG o.s.w.s.m.s.DefaultHandlerExceptionResolver - Resolving exception from handler [public void com.MyCompany.mocks.***.controller.***Controller.putJsonData(com.MyCompany.mocks.***.dto.***.RequestResponseWrapper)]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported 
9769 [qtp333509504-16] DEBUG o.s.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'Spring MVC Dispatcher Servlet': assuming HandlerAdapter completed request handling 
9769 [qtp333509504-16] TRACE o.s.web.servlet.DispatcherServlet - Cleared thread-bound request context: [POST //localhost:8087/MyCompanyMocks/mvc/***/sendData.json]@883530709 org.eclipse.jetty.server.Request@34a99bd5 
9769 [qtp333509504-16] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request 
9769 [qtp333509504-16] TRACE o.s.w.c.s.XmlWebApplicationContext - Publishing event in WebApplicationContext for namespace 'Spring MVC Dispatcher Servlet-servlet': ServletRequestHandledEvent: url=[/MyCompanyMocks/mvc/***/sendData.json]; client=[0:0:0:0:0:0:0:1]; method=[POST]; servlet=[Spring MVC Dispatcher Servlet]; session=[null]; user=[null]; time=[5027ms]; status=[OK] 

我花了很多时间来调试spring但我无法理解我的错误。

2 个答案:

答案 0 :(得分:4)

我发现了问题原因

我有两个名称为SkuSKU的字段,并且设置了相应的方法。

杰克逊不能接受以下命名的混乱

P.S。 spring提供了令人困惑的错误消息

答案 1 :(得分:0)

我还发现错误代码具有误导性,因为当Spring无法将您的@RequestBody类(以下示例中的TheEntity)转换为json时,通常会发生错误代码。

@PostMapping
public ResponseEntity<KerasModel> createNewEntity(@RequestBody TheEntity theEntity){
    return new ResponseEntity(entityService.createNew(theEntity),HttpStatus.CREATED);
}

您可能需要使用@Json注释所有非@Entity类型的字段。