2

Inside @RestController I have a @RequestMapping which works except I'm getting 406 in client while trying to return POJO class ResponseVO

@RequestMapping(value = "path", method = RequestMethod.POST
            , produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody ResponseEntity<GenericResponse> path(

...
ResponseVO responseVO = new responseVO();
return new ResponseEntity<>(responseVO, HttpStatus.OK);

I'm sending POST with JSON body, My request headers:

Connection: keep-alive
Content-Type: application/json
Accept: */*
Content-Length: 58
Host: localhost:8080
User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_151)

Response headers:

HTTP/1.1 406 Not Acceptable
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS, DELETE
Access-Control-Max-Age: 3600
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Authorization, Origin, Accept, Access-Control-Request-Method, Access-Control-Request-Headers
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1067
Ori Marko
  • 56,308
  • 23
  • 131
  • 233

3 Answers3

3

when you use @RestController it automatically means that you are annotating @Controller and @ResponseBodyin spring boot. So you do not explicitly need to add @ResponseBody annotation on your method. Also @ResponseBody annotation is added above the method not with the method declaration. So even if you add this annotation properly your code should work fine. take a look below.

@RequestMapping(value = "path", method = RequestMethod.POST, produces =MediaType.APPLICATION_JSON_VALUE)
@ResponseBody 
public ResponseEntity<GenericResponse> path(

...
    ResponseVO responseVO = new responseVO();
    return new ResponseEntity<>(responseVO, HttpStatus.OK);
yerqueri
  • 75
  • 1
  • 10
1

First of all, remove @ResponseBody annotation from method because it's already included in @RestController annotation. Also remove produces attribute from @RequestMapping and check if issue gone.

Kamil W
  • 2,230
  • 2
  • 21
  • 43
1

You didn't set a consumes property in @RequestMapping.

But you can use @PostMapping for JSON instead, there are all values will be set by default.

just @PostMapping("path")

dehasi
  • 2,644
  • 1
  • 19
  • 31