【发布时间】:2017-10-21 03:22:04
【问题描述】:
我尝试发送 PUT 请求。我有 serialazible 类 和具有必要设置的控制器,我的请求有 contentType: "application/json"。
FlightDto:
public class FlightDto implements Serializable {
private int id;
private String navigation;
public FlightDto() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNavigation() {
return navigation;
}
public void setNavigation(String navigation) {
this.navigation = navigation;
}
}
PUT 请求:
function editNavigation() {
var flight={
id:idAction.replace('edit',''),
navigation:newNavigation
};
console.log(flight);
var prefix = '/airline/';
$.ajax({
type: 'PUT',
url: prefix +'flights/' + idAction.replace('edit',''),
data: JSON.stringify(flight),
contentType: "application/json",
success: function(receive) {
$("#adminTable").empty();
$("#informationP").replaceWith(receive);
$("#hiddenLi").removeAttr('style');
},
error: function() {
alert('Error edited flight');
}
});
}
console.log(飞行); - 对象{id:“7”,导航:“sdfdsfsd”}
飞行控制器:
@RequestMapping(value = prefix + "/flights/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String updateFlight(@PathVariable("id") String id, @RequestBody FlightDto flightDto) {
String returnText = "Flight edited successful";
String str1 = flightDto.getNavigation();
String str2 = id;
return returnText;
}
错误:
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
如何解决这个错误?
【问题讨论】:
-
您应该在 ajax 中添加
dataType: "json"。或者添加像headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }这样的标题
标签: java json ajax spring spring-mvc