簡體   English   中英

org.springframework.web.HttpMediaTypeNotSupportedException:不支持內容類型'application / json'(Ajax,Spring)

[英]org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported(Ajax, Spring)

我嘗試發送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”}

FlightController:

   @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"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM