Когда я отправляю JSON-пакет из jQuery в Spring RestController, у меня появляется много ошибок:
Весной:
Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: тип содержимого 'приложение/ json; charset = UTF-8 'не поддерживается]
в Chrome:
POST http://localhost/post 415
(анонимно)@ main.js: 11
Мой код jQuery:
$(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
});
});
});
Код My 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
}
}
Пользователь сущности:
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class User {
private String name;
private int age;
}