Announcement Announcement Module
Collapse
No announcement yet.
Posting JSON to REST API Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Posting JSON to REST API

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

    I'm using Jackson 1.9.5 with Spring MVC 3.1.1 to map the request/responses to my model entities.

    GETs are working fine in my Controller, but I cannot get POSTs to work.

    The following CURL throws an error:

    PHP Code:
    curl --X POST -"Content-Type:application/json" -"Accept:application/json" http://localhost:8080/PurchaseAPIServer/api/purchase -d "{"pan":122}"

    HTTP/1.1 400 Bad Request
    Server
    Apache-Coyote/1.1
    Content
    -Typetext/html;charset=utf-8
    Content
    -Length971
    Date
    Thu26 Apr 2012 21:29:56 GMT
    Connection
    close

    The request sent by the client was syntactically incorrect 
    (). 
    Here is the result of a GET:

    PHP Code:
    curl --"Content-Type:application/json" -"Accept:application/json" http://localhost:8080/PurchaseAPIServer/api/purchase

    HTTP/1.1 200 OK
    Server
    Apache-Coyote/1.1
    Content
    -Typeapplication/json
    Transfer
    -Encodingchunked
    Date
    Thu26 Apr 2012 21:19:55 GMT

    [{"id":1,"pan":111}] 
    PurchaseController:

    PHP Code:
    @Controller
    public class PurchaseController {

        @
    Autowired
        
    private IPurchaseService purchaseService;

        @
    RequestMapping(value "purchase"method RequestMethod.GET)
        @
    ResponseBody
        
    public final List<PurchasegetAll() {
            return 
    purchaseService.getAll();
        }

        @
    RequestMapping(value "purchase"method RequestMethod.POST)
        @
    ResponseStatusHttpStatus.CREATED )
        public 
    void create(@RequestBody final Purchase entity) {
            
    purchaseService.addPurchase(entity);
        }

    Model:

    PHP Code:
    @Entity
    @XmlRootElement
    public class Purchase implements Serializable {

        
    /**
         * 
         */
        
    private static final long serialVersionUID 6603477834338392140L;

        @
    Id
        
    @GeneratedValue(strategy GenerationType.AUTO)
        private 
    Long id;

        private 
    Long pan;

        public 
    Long getId() {
            return 
    id;
        }

        public 
    void setId(Long id) {
            
    this.id id;
        }

        public 
    Long getPan() {
            return 
    pan;
        }

        public 
    void setPan(Long pan) {
            
    this.pan pan;
        }


    AppConfig:

    PHP Code:
    @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;
        }
    }

    Any help greatly appreciated.
    Last edited by langer123; Apr 27th, 2012, 04:07 PM.

  • #2
    I added logback to do some debugging and found the following error:

    Code:
    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 had to add TRACE level debugging to org.springframework.web.servlet.mvc to see this
    I simply changed my CURL POST to the following and it worked...BOOM.

    Code:
    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

    Comment

    Working...
    X