skip to Main Content

I have a method that returns a fully formatted JSON as a String, ex { type: "", List1: []…}
I’m trying to return that in a way that the browser interprets it as a JSON (Firefox). I’ve tried specifying that it produces="text/plain" but no luck.
I can’t put it in a wrapper class as it’s already formatted.

I tried specifying produces="text/plain" and @ResponseBody. No luck, still interpreted as text, not JSON.

@GetMapping(value="/v1/Count", produces="text/plain"
@ResponseBody
public String getCount(@RequestParam...) {
   return "{type: "", List1: []...}";
}

3

Answers


  1. Please show us the controller method you told us about

    Login or Signup to reply.
  2. Try using produces=MediaType.APPLICATION_JSON_VALUE. For example annotate your controller method with:

    @GetMapping(value="/path", produces=MediaType.APPLICATION_JSON_VALUE)
    
    Login or Signup to reply.
  3. Try to specify the produces in the annotation @RequestMapping as application/json and it will help you. For example:

    @RestController
    public class TestController {
    
        @GetMapping(path = "/test", produces = MediaType.APPLICATION_JSON_VALUE) // equals 'produces = "application/json"'
        public ResponseEntity<String> test() {
            return ResponseEntity.ok("{}"); // put your string here
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search