0

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.

edit: For my purposes, using produces=MediaType.APPLICATION_JSON_VALUE worked.

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

4 Answers4

1

To explicitly control the return type wrap it in an ResponseEntity and specify the content type on that.

@GetMapping(value="/v1/Count", produces="text/plain"
@ResponseBody
public ResponeEntity<String> getCount(@RequestParam...) {
   return ResponseEntity.ok()
           .contentType(MediaType.APPLICATION_JSON)
           .body("{type: "", List1: []...}");
}

This will use the actual content type you provided instead of trying to infer it from the return type of the method.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
-1

Try using produces=MediaType.APPLICATION_JSON_VALUE. For example annotate your controller method with:

@GetMapping(value="/path", produces=MediaType.APPLICATION_JSON_VALUE)
Andrew
  • 11
  • 3
  • The `produces` is only for mappin requests it is NOT for controlling the resulting header. The fact that it is part of the `@RequestMapping` should give the mapping part away. – M. Deinum Jul 26 '23 at 06:26
  • Using produces=MediaType.APPLICATION_JSON_VALUE worked! Thanks!! – Luke Jones Jul 26 '23 at 15:03
-1

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
    }
}
Maksim Eliseev
  • 487
  • 1
  • 11
  • The `produces` is only for mappin requests it is NOT for controlling the resulting header. The fact that it is part of the `@RequestMapping` should give the mapping part away. – M. Deinum Jul 26 '23 at 06:26
  • I think, `consumes` is used to control the request header, in't it? @M.Deinum – Maksim Eliseev Jul 26 '23 at 06:55
  • I created an example on my [github](https://github.com/elimxim/spring-web-response-content-type-example) that demonstrates that the `prodcues` correctly sets the `Content-Type` as if it was set using the `ResponseEntity`. @M.Deinum – Maksim Eliseev Jul 26 '23 at 07:51
  • produces as well. Because you can have multiple methods for the same URL but for different content-types that are accepted (like one for JSON and another PDF, using the same model but different ways to produce the output). It doesn't enforce the output. – M. Deinum Jul 26 '23 at 10:43
  • Thank you for your reply! I understood my mistake in the answer. @M.Deinum – Maksim Eliseev Jul 26 '23 at 11:00
-1

You can try these things:

  1. replace produces=text/plain with produces=MediaType.APPLICATION_JSON_VALUE
  2. Use @RestController instead of @Controller, then remove @ResponseBody
  • The `produces` is only for mappin requests it is NOT for controlling the resulting header. The fact that it is part of the `@RequestMapping` should give the mapping part away. – M. Deinum Jul 26 '23 at 06:26
  • Using produces=MediaType.APPLICATION_JSON_VALUE worked! Thanks!! – Luke Jones Jul 26 '23 at 15:03