Posting JSON to REST API

后端 未结 9 860
-上瘾入骨i
-上瘾入骨i 2020-12-08 08:12

I\'m creating a REST API that will accept JSON requests.

I\'m testing it out using CURL:

curl -i -POST -H \'Accept: application/json\' -d \'{\"id\":1         


        
9条回答
  •  孤街浪徒
    2020-12-08 09:04

    As sdouglass suggested, Spring MVC automatically detects Jackson and sets up a MappingJacksonHttpMessageConverter to handle conversion to/from JSON. But I did need explicity configure the converter to get it to work as he also pointed out.

    I added the following and my CURL GET requests were working..Hooray.

    AppConfig.java

    @Configuration
    @ComponentScan(basePackages = "com.app")
    public class AppConfig {
    
        @Bean
        public AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter()
        {
            final AnnotationMethodHandlerAdapter annotationMethodHandlerAdapter = new AnnotationMethodHandlerAdapter();
            final MappingJacksonHttpMessageConverter mappingJacksonHttpMessageConverter = new MappingJacksonHttpMessageConverter();
    
            HttpMessageConverter[] httpMessageConverter = { mappingJacksonHttpMessageConverter };
    
            String[] supportedHttpMethods = { "POST", "GET", "HEAD" };
    
            annotationMethodHandlerAdapter.setMessageConverters(httpMessageConverter);
            annotationMethodHandlerAdapter.setSupportedMethods(supportedHttpMethods);
    
            return annotationMethodHandlerAdapter;
        }
    }
    


    curl -i -H "Content-Type:application/json" -H "Accept:application/json" http://localhost:8080/PurchaseAPIServer/api/purchase
    
    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Content-Type: application/json
    Transfer-Encoding: chunked
    Date: Thu, 26 Apr 2012 21:19:55 GMT
    
    [{"id":1,"pan":111}]
    



    But the following CURL POST was still not working (Never hitting the controller action and giving no console debug info.

    curl -i -X POST -H "Content-Type:application/json"  http://localhost:8080/PurchaseAPIServer/api/purchaseMe -d "{"id":2,"pan":122}"
    
    HTTP/1.1 400 Bad Request
    Server: Apache-Coyote/1.1
    Content-Type: text/html;charset=utf-8
    Content-Length: 971
    Date: Thu, 26 Apr 2012 21:29:56 GMT
    Connection: close
    
    The request sent by the client was syntactically incorrect ().
    



    So I added Logback to get some detailed debugging started.

    
    
        
            
                %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
                
            
        
    
        
            /home/thomas/springApps/purchaseapi.log
            
                %date %level [%thread] %logger{10} [%file:%line] %msg%n
                
            
        
    
        
    
        
        
         
         
    
        
        
        
    
        
            
        
    
    
    



    Adding TRACE level debugging to org.springframework.web.servlet.mvc gave me the answer to the problem.

    2012-04-28 14:17:44,579 DEBUG [http-bio-8080-exec-3] o.s.w.s.m.m.a.RequestResponseBodyMethodProcessor [AbstractMessageConverterMethodArgumentResolver.java:117] Reading [com.app.model.Purchase] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@74a14fed]
    2012-04-28 14:17:44,604 TRACE [http-bio-8080-exec-3] o.s.w.s.m.m.a.ServletInvocableHandlerMethod [InvocableHandlerMethod.java:159] Error resolving argument [0] [type=com.app.model.Purchase]
    HandlerMethod details: 
    Controller [com.app.controller.PurchaseController]
    Method [public void com.app.controller.PurchaseController.create(com.app.model.Purchase)]
    
    org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('p' (code 112)): was expecting double-quote to start field name
    



    I changed my CURL POSTs to the following an it all worked:

    curl -i -X POST -H "Content-Type:application/json" http://localhost:8080/PurchaseAPIServer/api/purchase -d '{"pan":11111}'
    HTTP/1.1 201 Created
    Server: Apache-Coyote/1.1
    Content-Length: 0
    Date: Sat, 28 Apr 2012 13:19:40 GMT
    

    Hopefully someone finds this useful.

提交回复
热议问题