这个错误' org.springframework.http.converter.HttpMessageNotReadableException:无法读取JSON:无法反序列化实例'意思?

时间:2014-07-22 11:14:58

标签: java spring rest

我正在尝试将JSON数组发送到我的服务器。我的JSON数组看起来像这样

["{"sourceAccountNo":"555555555555555","sourceBankCode":"GLBBNPKA","destinationBankCode":"GLBBNPKA","destinationBankAccountNo":"123456789111111","amount":5000,"narrationOne":"txn4 by maker 1","uniqueId":"ESW-COR:1405507591784"}"]

相应的服务器端方法是:

   @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);
        }

现在我的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;
    }

}

我的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;
    }

}

每当我尝试将上面提到的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.jackson.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.jackson.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.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:219)
    at org.codehaus.jackson.map.deser.StdDeserializationContext.mappingException(StdDeserializationContext.java:212)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromArray(BeanDeserializer.java:869)
    at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:597)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2725)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1916)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.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)

任何建议都会有很大的帮助。

编辑更新(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"
}

1 个答案:

答案 0 :(得分:4)

显然,JSON解析器无法解析您的JSON。 尝试在http://jsonlint.com/验证JSON以查看问题所在。

正如其他人所提到的,你在初始方括号内有一组多余的引号。

您必须删除这些。如果转换仍然不起作用,您可能在将JSON映射到对象时遇到其他问题,但不要混淆这两个问题并将引号放回去 - 您将无法使用该方法 - 您需要有效的JSON首先,您可以确保可以将有效的JSON反序列化为预期的对象。

也许JSON反序列化的单元测试是按顺序进行的?