简体   繁体   English

发布到Spring Controller给出404错误请求

[英]Post to Spring Controller giving 404 Bad Request

I have this ajax call 我有这个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);
      });

calling this Spring Controller 调用这个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);
};

calls this importer 将此进口商称为

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

I get this error message in my browser console 我在浏览器控制台中收到此错误消息

"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]"

I am using Jackson to try go from JSON to a Java DTO. 我正在使用Jackson尝试从JSON转换为Java DTO。 I am getting this error and don't know how to fix. 我收到此错误,不知道如何解决。

Ok - have your code look like this: 好的-您的代码如下所示:

@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);
};

Also, F12 your browser and view the outgoing message to make sure its legit JSON. 另外,F12您的浏览器并查看传出消息以确保其合法的JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM