错误1
{
"timestamp": 1529747704259,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'multipart/form-data;boundary=--------------------------634977992820316635754608;charset=UTF-8' not supported",
"path": "/girl/girls"
}
解决方法:postman必须选择raw JSON(application/json)
错误2
{
"timestamp": 1529747826005,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body is missing: public com.imooc.domain.Girl com.imooc.controller.GirlController.girlAdd(com.imooc.domain.Girl)",
"path": "/girl/girls"
}
Body不能为空, 可以为{}
错误3
通过ajax,向后台传对象时,@RequestBody 接收对象,一直报415的错误
415说明传的数据类型不对,一直尝试各种方法改错,都不对,在百度@RequestBody 时,知道它接收的是json字符串,而ajax传送的是json对象,类型不一致,改错方法是:
var user = {"id":userid,
"username":usernames,"account":accounts,
"place":places,"phone":phones,};
$.ajax({
type: "POST",
dataType: 'json',
contentType:"application/json",
data: JSON.stringify(user),
......
})
其中,dataType和contentType也必须设置成json格式的,而JSON.stringify(obj)这个函数可以把json对象转换为json字符串,这三个设置是必不可少的。