3

This is my curl method request maps to POJO that fails with 400:

CURL REQUEST:

curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d     '{"key":"XYZ","uniqueId":"ABCD"}' http://localhost:8080/api/rest/connect/tick/view.json
* Adding handle: conn: 0x1cb54c0
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x1cb54c0) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /api/rest/connect/tick/view.json HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: application/json
> Content-type: application/json
> Content-Length: 53
>
* upload completely sent off: 53 out of 53 bytes
< HTTP/1.1 400 Bad Request
* Server Apache-Coyote/1.1 is not blacklisted
< Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
< Content-Length: 971
< Date: Thu, 19 Sep 2013 13:16:29 GMT
< Connection: close    

APPLICATION-CONFIG.XML:

<context:annotation-config/>
<mvc:annotation-driven/>
followed by, Bean Definitions

CONTROLLER CLASS:

@RequestMapping(value="/tick/view.*", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
    public IConnectResponse tickConnectPost(@RequestBody final ConnectServiceBean     connectServiceBean) {

CONNECTSERVICEBEAN:

import java.io.Serializable;
public class ConnectServiceBean implements Serializable {
    private static final long serialVersionUID = 1L;
    private String key;
    private String uniqueId;
    //Getters and setters }

But is working for another method request maps to String:

curl -i -H "Content-Type: application/json" -X POST -d '{"JKL@user.com"}'     http://localhost:8080/api/rest/connect/tick/XYZ/view.json
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: no-cache
Cache-Control: no-cache, no-store, max-age=0
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json;charset=UTF-8
Content-Language: en-GB
Transfer-Encoding: chunked
Date: Thu, 19 Sep 2013 13:11:40 GMT

CONTROLLER:

@RequestMapping(value="/tick/{key}/view.*", method = RequestMethod.POST,      consumes="application/json", produces = "application/json; charset=utf-8")
    public IConnectResponse tickConnectPostString (
        @PathVariable("key") String key, @RequestBody String uniqueId
    ) {}

I felt that there is something wrong with Jackson Mapping. But I have all the necessary POM dependencies also I read in some other stackoverflow post where I need only for Jackson configurations for bean mappings(not sure though) org.codehaus.jackson jackson-core-asl 1.9.7
org.codehaus.jackson jackson-mapper-asl 1.9.7

Pradeep
  • 149
  • 4
  • 10
  • what is produces=MediaType.APPLICATION_JSON_VALUE? produces = "application/json; charset=utf-8") should work – Ali.Mojtahed Sep 19 '13 at 20:26
  • Hi Ali, Thanks I changed that as per your suggestion but no luck :( – Pradeep Sep 19 '13 at 20:49
  • Are you trying to ConnectServiceBean stringify it before send? please post ajax method – Ali.Mojtahed Sep 20 '13 at 12:43
  • 4
    Thanks Ali, As per the link (http://stackoverflow.com/questions/10323957/posting-json-to-rest-api/10324550#10324550) **I turned on log4j tracing on spring mvc and could see the parsing error. It was basically JSON input format was incorrect.** So did some trial and error and changed the crul request to make it work. **curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d {\"key\":\"XYZ\",\"uniqueId\":\"ABCD\"} http://localhost:8080/api/rest/connect/tick/view.json** – Pradeep Sep 20 '13 at 13:40
  • @Pradeep THIS!!!! {\"key\":\"XYZ\",\"uniqueId\":\"ABCD\"} Thank you! – ThinkingStiff Jun 14 '14 at 07:15
  • +1 @Pradeep for pointing out how to find the actual parse error log. In my case was not needed to set `TRACE` as per your reference. `DEBUG` on `org.springframework.web.servlet.mvc` was enough. – h7r Nov 16 '14 at 13:27
  • you could also use the `@ConrollerAdvice` annotation and override the messageUnreadalbe method and then decide what to do with the exception. – igreenfield Jun 16 '15 at 19:23

0 Answers0