天天看点

Spring Boot Http 406 error

错误情况

not acceptable
           
简单说明,是客户端的网络请求在服务器端找不到对应的格式,请求不可接受。

解决

造成这种问题的原因很多,我的环境是Spring boot 1.2.8 ,所以一般都是加了注解@RestController 的,所以再返回数据的时候会自动解析成json格式。

检查返回的实体类:

public class NetStatus {

    private String code;
    private String desc;
    public NetStatus(){}
    public NetStatus(String code, String desc){
        this.code = code;
        this.desc = desc;
    }
}
           

没有什么问题呢,相比于传统的bean 只是少了set get方法。

检查controller

@RequestMapping(value = "/createUser",method = RequestMethod.POST )
    public ResponseEntity<NetStatus> createUser(@RequestBody User user){
    ...
    }
           

没有什么问题啊,@RestController我是标注在类级别的。

最后经过尝试,终于发现,就是因为第一个在bean中没有加set和get方法。不符合spring boot 中传统的配置。浪费了一个下午。

public class NetStatus {

    private String code;
    private String desc;
    public NetStatus(){}
    public NetStatus(String code, String desc){
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
           
补全方法后,一切OK!

继续阅读