Wednesday, April 9, 2014

Could not read JSON: Unexpected character (''' (code 39))

FRAMEWORK : Spring 3.2.3.RELEASE
JACKSON VERSION : 2.2.3
REST API URL : http://localhost:8080/rest/api/dogs
METHOD : PUT

REQUEST:
curl -i -X PUT -H "Content-Type: application/json" -d '{"id":"1","name":"Labrador","ownerName":"Waseem Akram"}' http://localhost:8080/rest/api/dogs

RESPONSE:
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 968
Date: Wed, 09 Apr 2014 18:15:26 GMT
Connection: close

DOG BEAN: 
public class Dog {
private Long id;
private String name;
private String ownerName;

public Dog() {
}

public Dog(Long id, String name, String ownerName) {
this.id = id;
this.name = name;
this.ownerName = ownerName;
}

//All the getters and setters
}

DogsController Code:
@Controller
@RequestMapping(value = "dogs")
class DogsController {
@Autowired
DogService dogService;

       @RequestMapping(method = RequestMethod.PUT)
@ResponseStatus(HttpStatus.OK)
public void update(@RequestBody Dog dog) {
RestPreconditions.checkNotNull(dog);
dogService.update(dog);
}
}

SERVER DEBUG LOGS:
DEBUG RequestResponseBodyMethodProcessor - Reading [class org.rest.valueobject.Dog] as "application/json" using [org.springframework.http.converter.js
on.MappingJackson2HttpMessageConverter@12157135]
DEBUG ExceptionHandlerExceptionResolver - Resolving exception from handler [public void org.rest.controller.DogsController.update(org.rest.valueobject
.Dog)]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character (''' (code 39)): expected a vali
d value (number, String, array, object, 'true', 'false' or 'null')

RESOLUTION:
Changed the data part in curl command from
 '{"id":"1","name":"Labrador","ownerName":"Waseem Akram"}'
to
 "{\"id\":1,\"name\":\"Labrador\",\"ownerName\":\"Waseem Akram\"}"


curl -i -X PUT -H "Content-Type: application/json" -d
"{\"id\":1,\"name\":\"Labrador\",\"ownerName\":\"Waseem Akram\"}" http://localhost:8080/rest/api/dogs

3 comments:

  1. I was having this problem, you saved me, thanks

    ReplyDelete
  2. Woori Casino Login - Play on Mobile or Desktop
    The febcasino.com Woori Casino App will 바카라사이트 be available 출장안마 at Woori Casino on a mobile or desktop basis. To play https://octcasino.com/ on our mobile or desktop, you can also play with your desktop browser, worrione

    ReplyDelete