Search code examples
spring-mvc

Return JSON for ResponseEntity<String>


I have a method in my controller that should returns a String in JSON. It returns JSON for non primitive types:

@RequestMapping(value = "so", method = RequestMethod.GET, 
    produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity<String> so() {
    return new ResponseEntity<String>("This is a String", HttpStatus.OK);
}

The curl response is:

This is a String


Solution

  • @RequestMapping(value = "so", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody String so() {
        return "This is a String";
    }