gpt4 book ai didi

java - org.springframework.web.HttpMediaTypeNotSupportedException : Content type 'application/json' not supported(Ajax, Spring )

转载 作者:太空宇宙 更新时间:2023-11-04 11:28:23 25 4
gpt4 key购买 nike

我尝试发送 PUT 请求。我有serialazible类和具有必要设置的 Controller ,我的请求有contentType:“application/json”。

目的地航类:

 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”}

飞行 Controller :

   @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 属性在 header 中而不是在有效负载中定义内容类型

   data: JSON.stringify(flight),
headers:{contentType: "application/json"}

关于java - org.springframework.web.HttpMediaTypeNotSupportedException : Content type 'application/json' not supported(Ajax, Spring ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44096415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com