Javascript 内容类型';应用程序/json;字符集=UTF-8';不支持,当我尝试向Spring发送JSON时
当我从jQuery向Spring RestController发送JSON包时,我有很多错误: 春天: 已解决[org.springframework.web.HttpMediaTypeNotSupportedException:Content-type'application/json;charset=UTF-8'不受支持] 镀铬: 415后 (匿名)@main.js:11 我的jQuery代码:Javascript 内容类型';应用程序/json;字符集=UTF-8';不支持,当我尝试向Spring发送JSON时,javascript,java,jquery,spring,rest,Javascript,Java,Jquery,Spring,Rest,当我从jQuery向Spring RestController发送JSON包时,我有很多错误: 春天: 已解决[org.springframework.web.HttpMediaTypeNotSupportedException:Content-type'application/json;charset=UTF-8'不受支持] 镀铬: 415后 (匿名)@main.js:11 我的jQuery代码: $(document).ready(function() { $('#go').on('cli
$(document).ready(function() {
$('#go').on('click', function() {
var user = {
"name" : "Tom",
"age" : 23
};
$.ajax({
type: 'POST',
url: "http://localhost/post",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(user),
dataType: 'json',
async: true
});
});
});
我的Spring RestController代码:
@RestController
public class mainController {
@RequestMapping(value = "/post", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Object> postUser(@RequestBody User user){
System.out.println(user);
return new ResponseEntity<>("Success", HttpStatus.OK);
}
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User getStr(){
System.out.println("-------------------------");
return new User("Tom", 56); //'Get' Check
}
}
您在rest控制器中使用了错误的媒体类型,即
APPLICATION\u FORM\u URLENCODED\u VALUE
。在传递JSON请求时,使用MediaType.APPLICATION\u JSON\u UTF8\u VALUE
@RestController
public class mainController {
@RequestMapping(value = "/post", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Object> postUser(@RequestBody User user){
System.out.println(user);
return new ResponseEntity<>("Success", HttpStatus.OK);
}
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User getStr(){
System.out.println("-------------------------");
return new User("Tom", 56); //'Get' Check
}
}
@RestController
公共类主控制器{
@RequestMapping(value=“/post”,method=RequestMethod.post,
consumes=MediaType.APPLICATION_JSON,
products={MediaType.APPLICATION\u JSON\u UTF8\u VALUE})
公共响应姿态(@RequestBody User){
System.out.println(用户);
返回新的响应状态(“成功”,HttpStatus.OK);
}
@RequestMapping(value=“/get”,method=RequestMethod.get)
公共用户getStr(){
System.out.println(“---------------------------”);
返回新用户(“Tom”,56);/“Get”检查
}
}
APPLICATION\u FORM\u URLENCODED\u VALUE=“APPLICATION/x-www-FORM-URLENCODED”
后端希望将“APPLICATION/x-www-FORM-URLENCODED”
作为内容类型,您将发送“APPLICATION/json;charset=utf-8”
。你应该检查后端逻辑。非常感谢,你救了我。我可以再问一个问题吗,如何在后端发送POST请求中的文件?您可以使用FormData
从ajax传递数据,并使用MultipartFile
在后端获取数据。这里有一篇关于这方面的好文章
@RestController
public class mainController {
@RequestMapping(value = "/post", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public ResponseEntity<Object> postUser(@RequestBody User user){
System.out.println(user);
return new ResponseEntity<>("Success", HttpStatus.OK);
}
@RequestMapping(value = "/get", method = RequestMethod.GET)
public User getStr(){
System.out.println("-------------------------");
return new User("Tom", 56); //'Get' Check
}
}