4

I am trying to send a JSON array to my server. My JSON array looks like this

["{"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

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

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

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)

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:

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"
}
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
Smrita
  • 1,259
  • 3
  • 18
  • 38
  • [Similar question](http://stackoverflow.com/questions/19333106/jsonmappingexception-out-of-start-array-token). I think, the answer will be same. – injecto Jul 22 '14 at 11:20
  • @injecto I don't think the answer will be the same. – Dawood ibn Kareem Jul 22 '14 at 11:37
  • @DavidWallace I think I agree with you an do you have any idea why am I getting such errors? – Smrita Jul 22 '14 at 11:39
  • And I am using Grails to form JSON objects and array and it seems to return JSON array in the format That I have mentioned above. Is it making a malformed JSON array? – Smrita Jul 22 '14 at 11:40
  • 1
    I don't really know. But your JSON string looks a bit odd to me. Are you sure that those unescaped double quotes should be sitting there inside the square brackets like that? – Dawood ibn Kareem Jul 22 '14 at 11:44
  • Hey Its like those quotation marks are being scaped whil being sent to the server:).yet the error persists anyway. I should edit my question too – Smrita Jul 22 '14 at 11:49
  • Umm, now that you've edited it, it kind of looks worse. No way are those quotes nesting correctly. I think you need to remove the first one and the last one. – Dawood ibn Kareem Jul 22 '14 at 11:58
  • 1
    What you have is not valid JSON. There shouldn't be a quote between `[` and `{`. – Hot Licks Jul 22 '14 at 12:42

1 Answers1

4

Clearly, the JSON parser is unable to parse your JSON. Try validating the JSON at http://jsonlint.com/ to see what the problem is.

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.

Perhaps a unit test for the JSON deserialization is in order?

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67