Spring Boot406“不可接受”

exdqitrt  于 2023-10-20  发布在  Spring
关注(0)|答案(4)|浏览(137)

我正在尝试使用2.0.5.RELEASE版本的Spring Boot构建RESTful API。下面是我的控制器:

// 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;
    }

}

这是Spring MVC配置类:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer{

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

}

控制器应该响应JSON数据,我用Postman测试了控制器,控制器可以接收请求参数并正常工作,但Postman得到了一个奇怪的响应:

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

有人能帮忙吗?

l0oc07j2

l0oc07j21#

可以实现Filter接口
并设置header中的所有方法都可以接受

@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() {}


}
ikfrs5lh

ikfrs5lh2#

请确保在postman标题中使用Accept: application/json
如果完成上述操作,则尝试在方法签名中添加consumes= MediaType.APPLICATION_JSON_VALUE和products。

6kkfgxo0

6kkfgxo03#

请确保您有这些jar并在postman头中使用Header Accept: application/json

<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>
olhwl3o2

olhwl3o24#

我也遇到了同样的问题
1.添加jackson
1.使用@JsonInclude(JsonInclude.Include.NON_NULL)@JsonIgnoreProperties(忽略未知= true)实作
1.可序列化
但在dto中缺少@Getter。

相关问题