SpringMVC,为什么'@RequestBody String body'在添加MappingJackson2HttpMessageConverter后停止工作

时间:2020-06-05 10:19:15

标签: java json spring-mvc jackson

需要有关 jackson2 springMvc 的帮助。

问题:添加MappingJackson2HttpMessageConverter时,json解析失败(以前是解析的),并出现以下错误。

我在代码中使用以下@Controller方法:

@RequestMapping(value = "/process", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody
<T extends ApiRequest> ApiResponse process(HttpServletRequest httpRequest, @RequestBody String requestJson) {... }

在我决定将MappingJackson2HttpMessageConverter()添加到转换器之前,它一直运行良好。像这样:

    @Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    ObjectMapper mapper = new ObjectMapper();

    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    mapper.registerModule(new JavaTimeModule());

    mapper.registerModule(new Hibernate5Module());

    converters.add(new MappingJackson2HttpMessageConverter(mapper));
}

但是这样做之后,我的请求停止运行,并在日志中出现以下错误:

2020-06-05 12:40:01.518 WARN [qtp1892075175-21] (DefaultHandlerExceptionResolver.java:419) - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse err
or: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.
String` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 1]

不知道为什么。 我知道我仍然可以从servletInputStream获得尸体,但不想这么做。 希望smb可以使此类问题更加清晰。

非常感谢!

我使用Spring 5,jackson2。

有人问我一些其他日志,这里是(调试级别),但是没有错误或堆栈跟踪,不知道它们是否有帮助:

    2020-06-05 14:08:33.731DEBUG [qtp1557520822-17] (HttpChannel.java:643) - REQUEST for //dev-vend:9990/api/v1/process on HttpChannelOverHttp@5283ae73{r=1,c=false,a=IDLE,uri=//dev-vend:9990/api/v1/process}
    POST //dev-vend:9990/api/v1/process HTTP/1.1
    Host: dev-vend:9990
    User-Agent: curl/7.54.0
    Content-Type: application/json
    Accept: application/json
    X-AID: 77702
    Content-Length: 91
    2020-06-05 14:08:33.731DEBUG [qtp1557520822-17] (HttpConnection.java:359) - HttpConnection@1a50894c[p=HttpParser{s=CONTENT,0 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=IDLE,uri=//dev-vend:9990/api/v1/process}<-SocketChannelEndPoint@78d4ac8e{/172.16.21.40:62214<->/172.16.12.33:9990,OPEN,fill=-,flush=-,to=16/30000}{io=0/0,kio=0,kro=1}->HttpConnection@1a50894c[p=HttpParser{s=CONTENT,0 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=IDLE,uri=//dev-vend:9990/api/v1/process} parsed true HttpParser{s=CONTENT,0 of 91}
    2020-06-05 14:08:33.731DEBUG [qtp1557520822-17] (HttpChannel.java:302) - HttpChannelOverHttp@5283ae73{r=1,c=false,a=IDLE,uri=//dev-vend:9990/api/v1/process} handle //dev-vend:9990/api/v1/process
    2020-06-05 14:08:33.731DEBUG [qtp1557520822-17] (HttpChannelState.java:217) - handling HttpChannelState@630d22c8{s=IDLE a=NOT_ASYNC i=true r=IDLE w=false}
    2020-06-05 14:08:33.732DEBUG [qtp1557520822-17] (HttpChannel.java:315) - HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process} action DISPATCH
    2020-06-05 14:08:33.733DEBUG [qtp1557520822-17] (Server.java:519) - REQUEST POST /api/v1/process on HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process}
    2020-06-05 14:08:33.733DEBUG [qtp1557520822-17] (ContextHandler.java:1076) - scope null||/api/v1/process @ o.s.b.w.e.j.JettyEmbeddedWebAppContext@5e3a577a{/,[file:///tmp/jetty-docbase.28728569537940407.9990/],AVAILABLE}
    2020-06-05 14:08:33.733DEBUG [qtp1557520822-17] (ContextHandler.java:1153) - context=||/api/v1/process @ o.s.b.w.e.j.JettyEmbeddedWebAppContext@5e3a577a{/,[file:///tmp/jetty-docbase.28728569537940407.9990/],AVAILABLE}
    2020-06-05 14:08:33.734DEBUG [qtp1557520822-17] (SessionHandler.java:1559) - sessionHandler=org.eclipse.jetty.server.session.SessionHandler110517298==dftMaxIdleSec=1800
    2020-06-05 14:08:33.734DEBUG [qtp1557520822-17] (SessionHandler.java:1560) - session=null
    2020-06-05 14:08:33.738DEBUG [qtp1557520822-17] (ServletHandler.java:465) - servlet |/api/v1/process|null -> dispatcherServlet@7ef5559e==org.springframework.web.servlet.DispatcherServlet,jsp=null,order=-1,inst=true
    2020-06-05 14:08:33.745DEBUG [qtp1557520822-17] (ServletHandler.java:513) - chain=characterEncodingFilter->hiddenHttpMethodFilter->httpPutFormContentFilter->requestContextFilter->springSecurityFilterChain->httpTraceFilter->webMvcMetricsFilter->Jetty_WebSocketUpgradeFilter->dispatcherServlet@7ef5559e==org.springframework.web.servlet.DispatcherServlet,jsp=null,order=-1,inst=true
    2020-06-05 14:08:33.750DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter characterEncodingFilter
    2020-06-05 14:08:33.752DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter hiddenHttpMethodFilter
    2020-06-05 14:08:33.752DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter httpPutFormContentFilter
    2020-06-05 14:08:33.752DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter requestContextFilter
    2020-06-05 14:08:33.754DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter springSecurityFilterChain
    2020-06-05 14:08:33.760DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 1 of 12 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
    2020-06-05 14:08:33.760DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 2 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
    2020-06-05 14:08:33.761DEBUG [qtp1557520822-17] (HttpSessionSecurityContextRepository.java:174) - No HttpSession currently exists
    2020-06-05 14:08:33.761DEBUG [qtp1557520822-17] (HttpSessionSecurityContextRepository.java:116) - No SecurityContext was available from the HttpSession: null. A new one will be created.
    2020-06-05 14:08:33.767DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 3 of 12 in additional filter chain; firing Filter: 'HeaderWriterFilter'
    2020-06-05 14:08:33.767DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 4 of 12 in additional filter chain; firing Filter: 'LogoutFilter'
    2020-06-05 14:08:33.767DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', GET]
    2020-06-05 14:08:33.768DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:137) - Request 'POST /api/v1/process' doesn't match 'GET /logout
    2020-06-05 14:08:33.768DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', POST]
    2020-06-05 14:08:33.768DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:157) - Checking match of request : '/api/v1/process'; against '/logout'
    2020-06-05 14:08:33.768DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', PUT]
    2020-06-05 14:08:33.768DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:137) - Request 'POST /api/v1/process' doesn't match 'PUT /logout
    2020-06-05 14:08:33.768DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', DELETE]
    2020-06-05 14:08:33.769DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:137) - Request 'POST /api/v1/process' doesn't match 'DELETE /logout
    2020-06-05 14:08:33.769DEBUG [qtp1557520822-17] (OrRequestMatcher.java:72) - No matches found
    2020-06-05 14:08:33.769DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 5 of 12 in additional filter chain; firing Filter: 'UsernamePasswordAuthenticationFilter'
    2020-06-05 14:08:33.769DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:157) - Checking match of request : '/api/v1/process'; against '/login'
    2020-06-05 14:08:33.769DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 6 of 12 in additional filter chain; firing Filter: 'RequestCacheAwareFilter'
    2020-06-05 14:08:33.769DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 7 of 12 in additional filter chain; firing Filter: 'SecurityContextHolderAwareRequestFilter'
    2020-06-05 14:08:33.775DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 8 of 12 in additional filter chain; firing Filter: 'AnonymousAuthenticationFilter'
    2020-06-05 14:08:33.776DEBUG [qtp1557520822-17] (AnonymousAuthenticationFilter.java:100) - Populated SecurityContextHolder with anonymous token: 'org.springframework.security.authentication.AnonymousAuthenticationToken@4a762148: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 172.16.21.40; SessionId: null; Granted Authorities: ROLE_ANONYMOUS'
    2020-06-05 14:08:33.776DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 9 of 12 in additional filter chain; firing Filter: 'SessionManagementFilter'
    2020-06-05 14:08:33.776DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 10 of 12 in additional filter chain; firing Filter: 'ExceptionTranslationFilter'
    2020-06-05 14:08:33.776DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 11 of 12 in additional filter chain; firing Filter: 'FilterSecurityInterceptor'
    2020-06-05 14:08:33.777DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', GET]
    2020-06-05 14:08:33.777DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:137) - Request 'POST /api/v1/process' doesn't match 'GET /logout
    2020-06-05 14:08:33.778DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', POST]
    2020-06-05 14:08:33.778DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:157) - Checking match of request : '/api/v1/process'; against '/logout'
    2020-06-05 14:08:33.778DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', PUT]
    2020-06-05 14:08:33.778DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:137) - Request 'POST /api/v1/process' doesn't match 'PUT /logout
    2020-06-05 14:08:33.778DEBUG [qtp1557520822-17] (OrRequestMatcher.java:65) - Trying to match using Ant [pattern='/logout', DELETE]
    2020-06-05 14:08:33.779DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:137) - Request 'POST /api/v1/process' doesn't match 'DELETE /logout
    2020-06-05 14:08:33.779DEBUG [qtp1557520822-17] (OrRequestMatcher.java:72) - No matches found
    2020-06-05 14:08:33.779DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:157) - Checking match of request : '/api/v1/process'; against '/login*'
    2020-06-05 14:08:33.779DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:157) - Checking match of request : '/api/v1/process'; against '/api/**'
    2020-06-05 14:08:33.779DEBUG [qtp1557520822-17] (AbstractSecurityInterceptor.java:219) - Secure object: FilterInvocation: URL: /api/v1/process; Attributes: [permitAll]
    2020-06-05 14:08:33.779DEBUG [qtp1557520822-17] (AbstractSecurityInterceptor.java:348) - Previously Authenticated: org.springframework.security.authentication.AnonymousAuthenticationToken@4a762148: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails@957e: RemoteIpAddress: 172.16.21.40; SessionId: null; Granted Authorities: ROLE_ANONYMOUS
    2020-06-05 14:08:33.790DEBUG [qtp1557520822-17] (AffirmativeBased.java:66) - Voter: org.springframework.security.web.access.expression.WebExpressionVoter@2e99f07b, returned: 1
    2020-06-05 14:08:33.791DEBUG [qtp1557520822-17] (AbstractSecurityInterceptor.java:243) - Authorization successful
    2020-06-05 14:08:33.791DEBUG [qtp1557520822-17] (AbstractSecurityInterceptor.java:256) - RunAsManager did not change Authentication object
    2020-06-05 14:08:33.794DEBUG [qtp1557520822-17] (FilterChainProxy.java:328) - /api/v1/process at position 12 of 12 in additional filter chain; firing Filter: 'FilterChainProxy'
    2020-06-05 14:08:33.794DEBUG [qtp1557520822-17] (AntPathRequestMatcher.java:157) - Checking match of request : '/api/v1/process'; against '/controller/**'
    2020-06-05 14:08:33.795DEBUG [qtp1557520822-17] (FilterChainProxy.java:202) - /api/v1/process has no matching filters
    2020-06-05 14:08:33.795DEBUG [qtp1557520822-17] (FilterChainProxy.java:313) - /api/v1/process reached end of additional filter chain; proceeding with original chain
    2020-06-05 14:08:33.795DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter httpTraceFilter
    2020-06-05 14:08:33.802DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter webMvcMetricsFilter
    2020-06-05 14:08:33.808DEBUG [qtp1557520822-17] (DefaultSingletonBeanRegistry.java:213) - Creating shared instance of singleton bean 'mvcHandlerMappingIntrospector'
    2020-06-05 14:08:33.821DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:313) - Looking up handler method for path /api/v1/process
    2020-06-05 14:08:33.828DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:323) - Did not find handler method for [/api/v1/process]
    2020-06-05 14:08:33.828DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:313) - Looking up handler method for path /api/v1/process
    2020-06-05 14:08:33.829DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:323) - Did not find handler method for [/api/v1/process]
    2020-06-05 14:08:33.829DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:313) - Looking up handler method for path /api/v1/process
    2020-06-05 14:08:33.830DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:320) - Returning handler method [public <T> ru.cwt.micro.api.model.res.ApiResponse ru.cwt.micro.api.controller.ApiController.process(javax.servlet.http.HttpServletRequest,java.lang.String)]
    2020-06-05 14:08:33.836DEBUG [qtp1557520822-17] (ServletHandler.java:1618) - call filter Jetty_WebSocketUpgradeFilter
    2020-06-05 14:08:33.837DEBUG [qtp1557520822-17] (ServletHandler.java:1649) - call servlet dispatcherServlet@7ef5559e==org.springframework.web.servlet.DispatcherServlet,jsp=null,order=-1,inst=true
    2020-06-05 14:08:33.838DEBUG [qtp1557520822-17] (DispatcherServlet.java:891) - DispatcherServlet with name 'dispatcherServlet' processing POST request for [/api/v1/process]
    2020-06-05 14:08:33.844DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:313) - Looking up handler method for path /api/v1/process
    2020-06-05 14:08:33.849DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:323) - Did not find handler method for [/api/v1/process]
    2020-06-05 14:08:33.849DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:313) - Looking up handler method for path /api/v1/process
    2020-06-05 14:08:33.849DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:323) - Did not find handler method for [/api/v1/process]
    2020-06-05 14:08:33.849DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:313) - Looking up handler method for path /api/v1/process
    2020-06-05 14:08:33.850DEBUG [qtp1557520822-17] (AbstractHandlerMethodMapping.java:320) - Returning handler method [public <T> ru.cwt.micro.api.model.res.ApiResponse ru.cwt.micro.api.controller.ApiController.process(javax.servlet.http.HttpServletRequest,java.lang.String)]
    2020-06-05 14:08:33.850DEBUG [qtp1557520822-17] (OpenEntityManagerInViewInterceptor.java:88) - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
    2020-06-05 14:08:33.888DEBUG [qtp1557520822-17] (HttpConnection.java:354) - HttpConnection@1a50894c[p=HttpParser{s=CONTENT,0 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process}<-SocketChannelEndPoint@78d4ac8e{/172.16.21.40:62214<->/172.16.12.33:9990,OPEN,fill=-,flush=-,to=173/30000}{io=0/0,kio=0,kro=1}->HttpConnection@1a50894c[p=HttpParser{s=CONTENT,0 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process} parse HeapByteBuffer@58ee971e[p=171,l=262,c=8192,r=91]={POST /api/v1/proc...-Length: 91\r\n\r\n<<<{\n    "uuid":"164...hment-result"\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00} {}
    2020-06-05 14:08:33.889DEBUG [qtp1557520822-17] (HttpParser.java:1342) - parseNext s=CONTENT HeapByteBuffer@58ee971e[p=171,l=262,c=8192,r=91]={POST /api/v1/proc...-Length: 91\r\n\r\n<<<{\n    "uuid":"164...hment-result"\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}
    2020-06-05 14:08:33.889DEBUG [qtp1557520822-17] (HttpChannel.java:651) - HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process} onContent Content@89a4d20{HeapByteBufferR@68f81b03[p=171,l=262,c=8192,r=91]={POST /api/v1/proc...-Length: 91\r\n\r\n<<<{\n    "uuid":"164...hment-result"\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}}
    2020-06-05 14:08:33.894DEBUG [qtp1557520822-17] (HttpInput.java:607) - HttpInputOverHTTP@5f89ed3[c=0,q=0,[0]=null,s=STREAM] addContent Content@89a4d20{HeapByteBufferR@68f81b03[p=171,l=262,c=8192,r=91]={POST /api/v1/proc...-Length: 91\r\n\r\n<<<{\n    "uuid":"164...hment-result"\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}}
    2020-06-05 14:08:33.894DEBUG [qtp1557520822-17] (HttpParser.java:1685) - CONTENT --> END
    2020-06-05 14:08:33.895DEBUG [qtp1557520822-17] (HttpChannel.java:659) - HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process} onContentComplete
    2020-06-05 14:08:33.895DEBUG [qtp1557520822-17] (HttpChannel.java:675) - HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process} onRequestComplete
    2020-06-05 14:08:33.895DEBUG [qtp1557520822-17] (HttpInput.java:607) - HttpInputOverHTTP@5f89ed3[c=0,q=1,[0]=EOF,s=STREAM] addContent EOF
    2020-06-05 14:08:33.898DEBUG [qtp1557520822-17] (HttpConnection.java:359) - HttpConnection@1a50894c[p=HttpParser{s=END,91 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process}<-SocketChannelEndPoint@78d4ac8e{/172.16.21.40:62214<->/172.16.12.33:9990,OPEN,fill=-,flush=-,to=181/30000}{io=0/0,kio=0,kro=1}->HttpConnection@1a50894c[p=HttpParser{s=END,91 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process} parsed false HttpParser{s=END,91 of 91}
    2020-06-05 14:08:33.898DEBUG [qtp1557520822-17] (HttpInput.java:297) - HttpInputOverHTTP@5f89ed3[c=1,q=1,[0]=EOF,s=STREAM] read 1 from Content@89a4d20{HeapByteBufferR@68f81b03[p=172,l=262,c=8192,r=90]={POST /api/v1/proc...Length: 91\r\n\r\n{<<<\n    "uuid":"164b...hment-result"\n}>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}}
    2020-06-05 14:08:33.899DEBUG [qtp1557520822-17] (AbstractMessageConverterMethodArgumentResolver.java:201) - Read [class java.lang.String] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@394253a]
    2020-06-05 14:08:33.906DEBUG [qtp1557520822-17] (HttpInput.java:297) - HttpInputOverHTTP@5f89ed3[c=91,q=1,[0]=EOF,s=STREAM] read 90 from Content@89a4d20{HeapByteBufferR@68f81b03[p=262,l=262,c=8192,r=0]={POST /api/v1/proc...hment-result"\n}<<<>>>\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00...\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00}}
    2020-06-05 14:08:33.906DEBUG [qtp1557520822-17] (HttpConnection.java:203) - releaseRequestBuffer HttpConnection@1a50894c[p=HttpParser{s=END,91 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process}<-SocketChannelEndPoint@78d4ac8e{/172.16.21.40:62214<->/172.16.12.33:9990,OPEN,fill=-,flush=-,to=191/30000}{io=0/0,kio=0,kro=1}->HttpConnection@1a50894c[p=HttpParser{s=END,91 of 91},g=HttpGenerator@3249a9a4{s=START}]=>HttpChannelOverHttp@5283ae73{r=1,c=false,a=DISPATCHED,uri=//dev-vend:9990/api/v1/process}
    2020-06-05 14:08:33.917DEBUG [qtp1557520822-17] (InvocableHandlerMethod.java:173) - Could not resolve parameter [1] in public <T> ru.cwt.micro.api.model.res.ApiResponse ru.cwt.micro.api.controller.ApiController.process(javax.servlet.http.HttpServletRequest,java.lang.String): JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]
    2020-06-05 14:08:33.918DEBUG [qtp1557520822-17] (AbstractHandlerExceptionResolver.java:137) - Resolving exception from handler [public <T> ru.cwt.micro.api.model.res.ApiResponse ru.cwt.micro.api.controller.ApiController.process(javax.servlet.http.HttpServletRequest,java.lang.String)]: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]
    2020-06-05 14:08:33.919DEBUG [qtp1557520822-17] (AbstractHandlerExceptionResolver.java:137) - Resolving exception from handler [public <T> ru.cwt.micro.api.model.res.ApiResponse ru.cwt.micro.api.controller.ApiController.process(javax.servlet.http.HttpServletRequest,java.lang.String)]: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]
    2020-06-05 14:08:33.920DEBUG [qtp1557520822-17] (AbstractHandlerExceptionResolver.java:137) - Resolving exception from handler [public <T> ru.cwt.micro.api.model.res.ApiResponse ru.cwt.micro.api.controller.ApiController.process(javax.servlet.http.HttpServletRequest,java.lang.String)]: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]
    2020-06-05 14:08:33.920 WARN [qtp1557520822-17] (DefaultHandlerExceptionResolver.java:419) - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

1 个答案:

答案 0 :(得分:2)

当您使用configureMessageConverters并添加到List<HttpMessageConverter<?>>时,您将告诉Spring MVC您添加的实例是唯一需要的HttpMessageConverter。结果,它不会添加其维护的默认值。 Javadoc声明

配置HttpMessageConverters以用于读取或写入 请求或响应的正文。如果未添加任何转换器, 默认的转换器列表已注册。

请注意,将转换器添加到列表中会关闭默认转换器 注册。简单添加转换器而不影响默认值 注册,考虑使用方法 改为extendMessageConverters(java.util.List)

这些默认值之一是StringHttpMessageConverter,它实际上可以将任何请求正文内容类型解析为String。由于您重写了该列表,因此该列表将不会被注册,并且Spring MVC将无法填充您的@RequestBody String参数。

按照Javadoc中的建议,您可以使用extendMessageConverters(java.util.List)添加自定义MappingJackson2HttpMessageConverter(尽管您可能希望替换现有的默认MappingJackson2HttpMessageConverter实例),也可以添加将StringHttpMessageConverter移到您自己的列表中。