Java org.springframework.web.HttpMediaTypeNotSupportedException:内容类型';application/json';不支持(Ajax、Spring)
我正在尝试发送PUT请求。我有Serializable类和控制器以及必要的设置,我的请求有contentType:“application/json” 飞行目的地:Java org.springframework.web.HttpMediaTypeNotSupportedException:内容类型';application/json';不支持(Ajax、Spring),java,json,ajax,spring,spring-mvc,Java,Json,Ajax,Spring,Spring Mvc,我正在尝试发送PUT请求。我有Serializable类和控制器以及必要的设置,我的请求有contentType:“application/json” 飞行目的地: public class FlightDto implements Serializable { private int id; private String navigation; public FlightDto() { super(); } public int g
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;
}
}
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');
}
});
}
@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
放置请求:
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;
}
}
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');
}
});
}
@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
控制台日志(航班);-对象{id:“7”,导航:“sdfdsfsd”}
飞行控制器:
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;
}
}
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');
}
});
}
@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
错误:
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;
}
}
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');
}
});
}
@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
如何修复此错误?您需要在标题中定义内容类型,而不是使用headers属性在负载中定义内容类型
data: JSON.stringify(flight),
headers:{contentType: "application/json"}
您需要在头中定义内容类型,而不是使用headers属性在负载中定义内容类型
data: JSON.stringify(flight),
headers:{contentType: "application/json"}
您应该在ajax中添加
数据类型:“json”
。或者添加头,如头:{'Accept':'application/json','Content Type':'application/json'}
您应该在ajax中添加数据类型:“json”
。或者添加标题,如headers:{'Accept':'application/json','Content Type':'application/json'}
changed:headers:{contentType:'application/json'}。为什么我只有警报(“错误编辑航班”);现在?更改:标题:{contentType:“application/json”}。为什么我只有警报(“错误编辑航班”);现在?