Spring Boot Jackson
Spring boot uses jackson to serialize the object and deserialize a string as an object.
an example of serialization
The following code will automatically convert the ResourceDefinition object into a json
@RequestMapping(value = "/select", method = RequestMethod.GET)
public ResourceDefinition select(@RequestParam("rid") String rid) {
log.info(String.format("[*] select resource Rid: %s", rid));
ResourceDefinition resourceDefinition = resourceService.getResourceDefinition(rid);
// if the target resource definition is not found, then null content will
// be returned
log.info(String.format("[*] select resource definition: %s", resourceDefinition));
return (resourceDefinition);
}
an example of deserialization
The following code use objectMapper ( objectMapper = new ObjectMapper()) which is from jackson to deserialize the json into an ojbect.
@RequestMapping(value = "/update", method = RequestMethod.POST)
public MsgResponseSimple update(@RequestParam("rid") String rid, @RequestParam("definition") String definition) throws Exception {
MsgResponseSimple msgResponseSimple;
ResourceDefinition resourceDefinition = objectMapper.readValue(definition, ResourceDefinition.class);
System.out.println("/update " + resourceDefinition);
try {
resourceService.setResourceDefinitionRepository(rid, resourceDefinition);
msgResponseSimple = new MsgResponseSimple(200, "OK");
} catch (Exception e) {
msgResponseSimple = new MsgResponseSimple(201, "Fail");
}
return msgResponseSimple;
}