Java Post到Spring控制器发出404错误请求
我接到这个电话Java Post到Spring控制器发出404错误请求,java,ajax,spring,spring-mvc,jackson,Java,Ajax,Spring,Spring Mvc,Jackson,我接到这个电话 $.ajax({ headers: { 'Content-Type': 'application/json' }, url: urlString, type: 'POST', dataType: 'json', data: JSON.stringify({ "field1": 1, "field2": "foo", "field3": "m
$.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控制器
@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转换为JavaDTO。我遇到此错误,不知道如何修复。好的-让您的代码如下所示:
@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合法。Try生成并使用“application/JSON”作为请求映射参数。工作非常出色!荣誉
headers=“Accept=application/json”
我认为是缺少的部分