0

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

Community
  • 1
  • 1
Marged
  • 10,577
  • 10
  • 57
  • 99
  • Do you really need to save CPU time? http://en.wikiquote.org/wiki/William_Wulf – Leon Jan 19 '15 at 07:30
  • Agreed. Don't optimize until you know you need to, which means having profiled it to show that this is an actual problem... – Graham Jan 19 '15 at 07:47
  • I strongly believe that a factor of more than 50 justifies to cache this. When taken from the cache my jMeter shows me an average of something around 30ms. When taking the values from storage and then rendering it into json I surpass the 1000ms with sometimes reaching over 2 seconds (depending on the storage). Is that enough ? ;-) And just to mention: I have more than 10,000 users that will use this webservice. – Marged Jan 19 '15 at 10:09
  • Take a look at this post [Cache HTTP Response in Spring MVC Rest service][1]. I think you will find it helpful. [1]: http://stackoverflow.com/questions/5114666/cache-http-response-in-spring-mvc-rest-service – crabe Jan 19 '15 at 13:30

0 Answers0