Building on an earlier post I created a webservice that looks like this:
@RequestMapping(value = "/getFoo", method = RequestMethod.GET, produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@Cacheable(value = "foo")
public ResponseEntity<String> getFoo(
@RequestParam(value = "fooId", defaultValue = "") String fooId )
{
if (fooId.isEmpty() ) {
return new ResponseEntity<String>(HttpStatus.BAD_REQUEST);
}
HugeValue foo = delivery.getHugeValue(fooId);
ObjectMapper mapper = new ObjectMapper();
String json = "";
try {
json = mapper.writeValueAsString(foo);
} catch (JsonProcessingException e) {
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>(json, HttpStatus.OK);
}
The mapper.writeValueAsString(foo)
part works but to be honest I don't like the way the method looks now ;-)
I chose this approach because the JSON does not need to be generated again as soon as it has been put into the cache (this was my main objective because the JSON is quite big and I want to save CPU time)
Do you know alternatives to my approach ? I don't like my current approach because it no longer is based on the "automagic" marshalling from object to JSON spring normally provides. Now I am tied to jackson and I would like to have it more generic. I use spring boot 1.2 and spring 4.1