Java 这个错误“org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法反序列化实例”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24885870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does this error 'org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance' mean?
提问by Smrita
I am trying to send a JSON array to my server. My JSON array looks like this
我正在尝试将 JSON 数组发送到我的服务器。我的 JSON 数组如下所示
["{"sourceAccountNo":"555555555555555","sourceBankCode":"GLBBNPKA","destinationBankCode":"GLBBNPKA","destinationBankAccountNo":"123456789111111","amount":5000,"narrationOne":"txn4 by maker 1","uniqueId":"ESW-COR:1405507591784"}"]
And the corresponding server side method is :
而对应的服务器端方法是:
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = "/xxx", method = RequestMethod.POST)
public ResponseEntity<BulkCorporatePaymentResponse> xxx(@RequestBody BulkCorporatePaymentRequest corporatePaymentRequestList) {
BulkCorporatePaymentResponse response = null;
try {
response = corporateApi.processBulkFundTransfer(corporatePaymentRequestList);
} catch (Exception e) {
}
return new ResponseEntity<BulkCorporatePaymentResponse>(response, HttpStatus.OK);
}
Now my BulkCorporatePaymentResponse and BulkCorporatePaymentRequest look like this
现在我的 BulkCorporatePaymentResponse 和 BulkCorporatePaymentRequest 看起来像这样
public class BulkCorporatePaymentResponse implements Serializable {
private static final long serialVersionUID = -2026566288888889L;
List<CorporatePaymentResponse> paymentResponseList;
public List<CorporatePaymentResponse> getPaymentResponseList() {
return paymentResponseList;
}
public void setPaymentResponseList(List<CorporatePaymentResponse> paymentResponseList) {
this.paymentResponseList = paymentResponseList;
}
}
public class BulkCorporatePaymentRequest implements Serializable {
private static final long serialVersionUID = -202656628880099889L;
private List<CorporatePaymentRequest> paymentRequestlist;
public List<CorporatePaymentRequest> getPaymentRequestlist() {
return paymentRequestlist;
}
public void setPaymentRequestlist(List<CorporatePaymentRequest> paymentRequestlist) {
this.paymentRequestlist = paymentRequestlist;
}
}
And my CorporatePaymentRequest looks liks this
我的 CorporatePaymentRequest 看起来像这样
public class CorporatePaymentRequest implements Serializable {
private static final long serialVersionUID = 1346864799470440670L;
private String sourceAccountNo;
private String sourceBankCode;
private String destinationBankCode;
private String destinationBankAccountNo;
private String corporateName;
private double amount;
private String remarks;
private String narrationOne;
private String narrationTwo;
private String uniqueId;
public String getSourceAccountNo() {
return sourceAccountNo;
}
public void setSourceAccountNo(String sourceAccountNo) {
this.sourceAccountNo = sourceAccountNo;
}
public String getSourceBankCode() {
return sourceBankCode;
}
public void setSourceBankCode(String sourceBankCode) {
this.sourceBankCode = sourceBankCode;
}
public String getDestinationBankCode() {
return destinationBankCode;
}
public void setDestinationBankCode(String destinationBankCode) {
this.destinationBankCode = destinationBankCode;
}
public String getDestinationBankAccountNo() {
return destinationBankAccountNo;
}
public void setDestinationBankAccountNo(String destinationBankAccountNo) {
this.destinationBankAccountNo = destinationBankAccountNo;
}
public String getCorporateName() {
return corporateName;
}
public void setCorporateName(String corporateName) {
this.corporateName = corporateName;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getNarrationOne() {
return narrationOne;
}
public void setNarrationOne(String narrationOne) {
this.narrationOne = narrationOne;
}
public String getNarrationTwo() {
return narrationTwo;
}
public void setNarrationTwo(String narrationTwo) {
this.narrationTwo = narrationTwo;
}
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
}
And whenever i try to pass the above mentioned json array to my server I get the following exception
每当我尝试将上述 json 数组传递到我的服务器时,我都会收到以下异常
Jul 22, 2014 4:53:38 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [esewarest] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Can not deserialize instance of com.esewa.server.corporate.schema.BulkCorporatePaymentRequest out of START_ARRAY token
at [Source: org.apache.catalina.connector.CoyoteInputStream@72ced13f; line: 1, column: 1]; nested exception is org.codehaus.Hymanson.map.JsonMappingException: Can not deserialize instance of com.esewa.server.corporate.schema.BulkCorporatePaymentRequest out of START_ARRAY token
at [Source: org.apache.catalina.connector.CoyoteInputStream@72ced13f; line: 1, column: 1]] with root cause
org.codehaus.Hymanson.map.JsonMappingException: Can not deserialize instance of com.esewa.server.corporate.schema.BulkCorporatePaymentRequest out of START_ARRAY token
at [Source: org.apache.catalina.connector.CoyoteInputStream@72ced13f; line: 1, column: 1]
at org.codehaus.Hymanson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.Hymanson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
at org.codehaus.Hymanson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
at org.codehaus.Hymanson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:869)
at org.codehaus.Hymanson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
at org.codehaus.Hymanson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2725)
at org.codehaus.Hymanson.map.ObjectMapper.readValue(ObjectMapper.java:1916)
at org.springframework.http.converter.json.MappingHymansonHttpMessageConverter.readInternal(MappingHymansonHttpMessageConverter.java:122)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:641)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:605)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:354)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:206)
at net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:179)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:311)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:116)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:101)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:139)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:182)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at com.esewa.server.web.filter.GWTCacheControlFilter.doFilter(GWTCacheControlFilter.java:36)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Any suggestion would be of great help.
任何建议都会有很大帮助。
UPDATE by editor (Mona Jalal): On a side note I get this error when posting raspi IP to Postman and click on the send button:
编辑器更新(Mona Jalal):附带说明,在将 raspi IP 发布到 Postman 并单击发送按钮时,我收到此错误:
144.92.129.230:8080/api/devices
{
"timestamp": 1457494478717,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body content is missing: org.springframework.web.method.HandlerMethod$HandlerMethodParameter@53cb13b3",
"path": "/api/devices"
}
回答by GreyBeardedGeek
Clearly, the JSON parser is unable to parse your JSON. Try validating the JSON at http://jsonlint.com/to see what the problem is.
显然,JSON 解析器无法解析您的 JSON。尝试在http://jsonlint.com/验证 JSON以查看问题所在。
As others have mentioned, you have an extraneous set of quotes just inside your initial square brackets.
正如其他人所提到的,您的初始方括号内有一组无关的引号。
You must remove these. If the conversion still doesn't work, you may have additional problems with the mapping of your JSON to your object, but don't confuse the two issues and put the quotes back - you will get nowhere with that approach - you need valid JSON first, then you can work on making sure that the valid JSON can be deserialized into the expected object.
您必须删除这些。如果转换仍然不起作用,您可能会在将 JSON 映射到对象时遇到其他问题,但不要混淆这两个问题并将引号放回原处 - 这种方法将一事无成 - 您需要有效的 JSON首先,您可以确保可以将有效的 JSON 反序列化为预期的对象。
Perhaps a unit test for the JSON deserialization is in order?
也许 JSON 反序列化的单元测试是有序的?