user592748
user592748

Reputation: 1234

Producing and consuming custom JSON Objects in Spring RESTful services

I have some JSON Objects that are more complex than the JSON representations of the java objects I have. I have methods that build these JSON Objects and I would like to return and consume these directly. I use org.json library to build my JSONs. I could get the GET method working by returning the JSON Object as a String. Is this the correct way to go about it?

@RequestMapping(value = "/getjson", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public String getJson() {
    JSONObject json = new JSONObject();
     JSONObject subJson = new JSONObject();
    subJson .put("key", "value");
    json.put("key", subJson);
    return json.toString();
}

Now I want to know how do I consume a JSON Object? As a string and convert it to a JSON Object?

    @RequestMapping(value = "/post", method = RequestMethod.POST, produces="application/json", consumes="application/json")
    @ResponseBody
    public String post(@RequestBody String json) {
        JSONObject obj = new JSONObject(json);
        //do some things with json, put some header information in json
        return obj.toString();
    }

Is this the correct way to go about my problem? I am a novice, so kindly point out anything that can be done better. Please note: I do not want to return POJOs.

Upvotes: 20

Views: 64653

Answers (4)

Reuben Tanner
Reuben Tanner

Reputation: 5565

I couldn't get the auto serialization and deserialization to work using the default Spring boot beans. In the end, this worked well for me after including Project Lombok and apache BeanUtils. Spring calls a String arg constructor if it can't automatically do the serialization.

@PostMapping("/create")
@ResponseBody
public User createUser(HttpServletRequest request, @RequestParam("user") User u) throws IOException {

    LOG.info("Got user! " + u);

    return u;
}


@ToString() @Getter() @Setter() @NoArgsConstructor()
public class User {
    private String email;
    private String bio;
    private String image;
    private String displayName;
    private String userId;
    private long lat;
    private long lng;

    public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
        ObjectMapper om = new ObjectMapper();
        User u = om.readValue(json, User.class);
        BeanUtils.copyProperties(this, u);
    }
}

http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi https://projectlombok.org/

Upvotes: 0

dReAmEr
dReAmEr

Reputation: 7196

I think using jackson library you can do something like below.

@RequestMapping(value = "/getjson", method = RequestMethod.GET, produces="application/json")
@ResponseBody
public String getJson() {
   //your logic
    ObjectMapper mapper = new ObjectMapper();
    return mapper.writeValueAsString(json);
}

@RequestMapping(value = "/post", method = RequestMethod.POST, produces="application/json", consumes="application/json")
@ResponseBody
public String post(@RequestBody String json) {
    POJO pj = new POJO();
    ObjectMapper mapper = new ObjectMapper();
    pj = mapper.readValue(json, POJO.class);

    //do some things with json, put some header information in json
    return mapper.writeValueAsString(pj);
}

Upvotes: 23

paul
paul

Reputation: 13471

I much rather the alternative to use Jackson with Spring mvc since you dont have to worry of the serialization and deserialization of your objects/json-json/object. But if you still want to the process I like to use gson of google.

http://www.javacreed.com/simple-gson-example/

Upvotes: 1

kemenov
kemenov

Reputation: 407

You can use jackson lib, jackson allows convert to/from json using spring mvc.

  1. Spring configure @ResponseBody JSON format
  2. Jackson 2.0 with Spring 3.1

Upvotes: 1

Related Questions