Announcement Announcement Module
No announcement yet.
restTemplate.postForObject does not pass json validation Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • restTemplate.postForObject does not pass json validation

    I have implemented a json java class, want to test if it is a valid json format. So I choose restTemplate to post this java class to to validate. I suppose the restTemplate will do all the correct Convert java class to JSON data(serialization) before it post a json string to . However, when run the test, I always got below errors returned from

    { "error": "A JSONObject text must begin with '{' at 1 [character 2 line 1]", "object_or_array": "object", "error_info": "This error came from the org.json reference parser.", "validate": false } It seems when restTemplate convert java class to JSON data, it does to add '{' and '}' around the generated string. Do I need to do something to make it add '{}'? I'm quite new to json and restTemplate. Hope someone of you in this forum could help me to figure out the reason of the problem I encountered. Thanks in advance. I have imported below jars in my build jackson-annotations-2.2.2.jar jackson-core-2.2.3.jar jackson-databind-2.2.2.jar spring-android-core-1.0.1.RELEASE.jar spring-android-rest-template-1.0.1.RELEASE.jar
    Below is my code:
    public class Greeting { private String id; private String content; public String getId() { return; } public void setId(String id) { = id ; } public String getContent() { return this.content; } public void setContent(String content) { this.content = content ; } } final String url = ""; //Greeting Greeting greeting = new Greeting(); greeting.setId("thisid"); greeting.setContent("thiscontent"); RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); Object result = restTemplate.postForObject(url, greeting, Object.class); return result;