Spring Boot 406 "Not Acceptable"

Viewed 7527

I am trying to build RESTful API using Spring Boot whose version is 2.0.5.RELEASE. Here's my controller:

// Just for test
@RestController
public class LoginController {

    @RequestMapping(value = "/user/login",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> login(@RequestParam(name = "username") String username,
                                   @RequestParam(name = "password") String password) {

        ResponseEntity<RESTResponse> response = null;

        if(username.equals("123") && password.equals("123")){
            // success
            response = new ResponseEntity<>(RESTResponse.generateResponse(
                    null, "successful", "Log in successfully."), HttpStatus.OK);
        } else {
            // failed
            response = new ResponseEntity<>(RESTResponse.generateResponse(
                    null, "failed", "Your username or password is incorrect."), HttpStatus.OK);
        }

        return response;
    }

}

And this is Spring MVC configuration class:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer{

    /**
     * CORS configuration
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins(ALL)
                .allowedMethods(ALL)
                .allowedHeaders(ALL)
                .allowCredentials(true);
    }


}

The controller is supposed to response JSON data. I used Postman to test the controller. The controller could receive the request parameters and work properly but the Postman got a weird response:

{
    "timestamp": "2018-09-16T05:55:14.860+0000",
    "status": 406,
    "error": "Not Acceptable",
    "message": "Could not find acceptable representation",
    "path": "/api/user/login"
}

Could anyone help?

4 Answers

Make sure you are using Accept: application/json in postman header.

If above is done then try adding consumes= MediaType.APPLICATION_JSON_VALUE also in the method signature along with produces.

you can implements Filter interface

and set the header with all method can be accepted

@Component
public class CORSFilter implements Filter{


     static Logger logger = LoggerFactory.getLogger(CORSFilter.class);

     @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }

        @Override
        public void doFilter(ServletRequest request, ServletResponse res, FilterChain chain) throws IOException, ServletException {
              HttpServletResponse response = (HttpServletResponse) res;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
            chain.doFilter(request, response);
            logger.info(request.getRemoteAddr());
        }

        public void destroy() {}



}

make sure you have these jars and use Header Accept: application/json in postman headers

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.1.1</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>

<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

I had the same issue and tried

  1. adding jackson
  2. using @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true)implements
  3. Serializable

but was missing @Getter in dto.

Related