javaspringspring-restcontrollerhttp-status-code-406

Spring RequestMapping in RestController returns 406


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

Solution

  • 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.