簡體   English   中英

發布到Spring Controller給出404錯誤請求

[英]Post to Spring Controller giving 404 Bad Request

我有這個ajax電話

$.ajax({
      headers: {
        'Content-Type': 'application/json'
      },
      url: urlString,
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify({
         "field1": 1,
         "field2": "foo",
         "field3": "meh"
      })
      })
      .done(function (dataFromServer) {
         //blah
      })
      .fail(function (jqXHR) {
           console.log(jqXHR);
      });

調用這個Spring Controller

@RequestMapping(value="more/updateThisTable", method=RequestMethod.POST)
public void updateThisTable(@RequestBody String jsonInput) throws JsonProcessingException, IOException {
    TableDTO t;
    TableImporter tImp = null;
    t= crewImp.getTableDTO(jsonInput);
    System.out.println(t);
    tableService.updateThisTable(t);
};

將此進口商稱為

public TableDTO getTableDTO(String json) throws JsonProcessingException, IOException{
    TableDTO tDTO = new TableDTO();
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(json);

    tDTO.setId(root.path("field1").asInt());
    tDTO.setCrewGroupId(root.path("field2").asText());
    tDTO.setName(root.path("field3").asText());

    return tDTO;
}

我在瀏覽器控制台中收到此錯誤消息

"Could not read JSON: Can not deserialize instance of java.lang.String out of START_OBJECT token↵ at [Source: java.io.PushbackInputStream@124b300; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token↵ at [Source: java.io.PushbackInputStream@124b300; line: 1, column: 1]"

我正在使用Jackson嘗試從JSON轉換為Java DTO。 我收到此錯誤,不知道如何解決。

好的-您的代碼如下所示:

@RequestMapping(value="more/updateThisTable", method=RequestMethod.POST, headers="Accept=application/json")
public void updateThisTable(@RequestBody TableDTO t) throws JsonProcessingException, IOException {
    System.out.println(t);
    tableService.updateThisTable(t);
};

另外,F12您的瀏覽器並查看傳出消息以確保其合法的JSON。

暫無
暫無

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

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