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

  • restTemplate.postForObject does not pass json validation validate.jsontest.com

    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 http://validate.jsontest.com/ 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 http://validate.jsontest.com/ . However, when run the test, I always got below errors returned from http://validate.jsontest.com/

    { "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 this.id; } public void setId(String id) { this.id = id ; } public String getContent() { return this.content; } public void setContent(String content) { this.content = content ; } } final String url = "http://validate.jsontest.com/?json="; //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;
Working...
X