今天遇到一个奇怪的错误:org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
springmvc已经在项目中用了一段时间了,这次新加的接口在进行post时候一直报这个错,百度后的解决方法是Entity中的get、set有问题,但是本人核实后发现get、set没有问题。
代码摘要:
ApiAuthorityBean:
public class ApiAuthorityBean extends BaseBean {
/** 注意:ManyToOne的get和set方法上需要@JsonBackReference,以免jackson陷入死循环 **/
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(value = { CascadeType.REFRESH })
@JoinColumn(name = "api_id")
private ApiBean api;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(value = { CascadeType.REFRESH })
@JoinColumn(name = "authority_id")
private AuthorityBean authority;
@JsonBackReference
public ApiBean getApi() {
return api;
}
@JsonBackReference
public void setApi(ApiBean api) {
this.api = api;
}
@JsonBackReference
public AuthorityBean getAuthority() {
return authority;
}
@JsonBackReference
public void setAuthority(AuthorityBean authority) {
this.authority = authority;
}
}
ApiBean:
public class ApiBean extends BaseBean {
@ApiModelProperty(value="api名称",required=true)
private String name;
@ApiModelProperty(value="api",required=true)
private String api;
@ApiModelProperty(value="拥有的功能",hidden=true)
@OneToMany(mappedBy = "api")
private List<ApiAuthorityBean> apiAuthorityList = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getApi() {
return api;
}
public void setApi(String api) {
this.api = api;
}
@JsonManagedReference
public List<ApiAuthorityBean> getApiAuthorityList() {
return apiAuthorityList;
}
@JsonManagedReference
public void setApiAuthorityList(List<ApiAuthorityBean> apiAuthorityList) {
this.apiAuthorityList = apiAuthorityList;
}
}
AuthorityBean:
public class AuthorityBean extends BaseBean {
@ApiModelProperty(value="功能名称",required=true)
private String name;
@ApiModelProperty(value="功能编码",required=true)
private String code;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
Controller:
@ApiOperation(value = "权限管理:新增接口", httpMethod = "POST", produces=MediaType.APPLICATION_JSON_VALUE, notes = "新增接口,返回新增成功的完整数据。")
@ApiResponses(value={@ApiResponse(code=201,message="Created",response=ApiBean.class)})
@RequestMapping(value="/api",method=RequestMethod.POST)
@ResponseBody
public String add(@ApiParam(value="Json参数",required=true)@RequestBody ApiBean bean) throws Exception{
经过反复测试发现,是ApiAuthorityBean中getAuthority()和setAuthority(AuthorityBean authority)多加了@JsonBackReference导致,修正后的ApiAuthorityBean:
public class ApiAuthorityBean extends BaseBean {
/** 注意:ManyToOne的get和set方法上需要@JsonBackReference,以免jackson陷入死循环 **/
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(value = { CascadeType.REFRESH })
@JoinColumn(name = "api_id")
private ApiBean api;
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@ManyToOne(fetch = FetchType.LAZY)
@Cascade(value = { CascadeType.REFRESH })
@JoinColumn(name = "authority_id")
private AuthorityBean authority;
@JsonBackReference
public ApiBean getApi() {
return api;
}
@JsonBackReference
public void setApi(ApiBean api) {
this.api = api;
}
public AuthorityBean getAuthority() {
return authority;
}
public void setAuthority(AuthorityBean authority) {
this.authority = authority;
}
}