HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported

今天遇到一个奇怪的错误: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;
	}
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值