java Spring REST @RequestBody 始终为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34084255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring REST @RequestBody is always empty
提问by Mr.wiseguy
So after I fixed the '415 media not supported' error (415 media not supported), I encountered a new issue. My RequestBody
is always empty. When I send a request via Chrome PostManI always get a serialized entity back with only null values. I am using Spring (4.2.3.RELEASE)
,Hymanson-databind (2.6.3)
and Hymanson-core (2.6.3)
in my project. I am using annotation based configuration in my project (@EnableWebMvc
to make spring automatically discover HTTPMessageConverters
).
因此,在我修复了“415 media not supported”错误(不支持 415 media)之后,我遇到了一个新问题。我RequestBody
的总是空的。当我通过Chrome PostMan发送请求时,我总是得到一个只有空值的序列化实体。我在我的项目中使用Spring (4.2.3.RELEASE)
,Hymanson-databind (2.6.3)
和Hymanson-core (2.6.3)
。我在我的项目中使用基于注释的配置(@EnableWebMvc
使 spring 自动发现 HTTPMessageConverters
)。
Other posts
其他职位
I am aware of other posts on stackoverflow, with almost the same problem. Yet they did not provide me with an answer. Also the most of the posts are for older Spring versions (pre 4.0), so some things are quite different now.
我知道关于 stackoverflow 的其他帖子,几乎有同样的问题。然而他们没有给我答案。此外,大多数帖子都是针对较旧的 Spring 版本(4.0 之前的),所以现在有些事情已经大不相同了。
Similar posts:
类似的帖子:
@RepsonseBody is always empty in Spring
Spring's @RequestBody providing empty string on POST
Spring 的 @RequestBody 在 POST 上提供空字符串
Using HttpServletRequest request
Spring controller
弹簧控制器
In my Spring @RestController
I have the following code:
在我的 Spring 中,@RestController
我有以下代码:
@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
public UserLocation updateUserLocation(@PathVariable("id") int id, UserLocation user) {
return user;
}
I am using a separate model class (UserLocation
) for my data binding. This is because this way I have more control of the data my API sends and recieves.
我使用单独的模型类 ( UserLocation
) 进行数据绑定。这是因为这样我可以更好地控制 API 发送和接收的数据。
Databinding class (UserLocation)
数据绑定类(UserLocation)
The UserLocation class consits of 3 properties with a constructor and the required getters and setters. (I could make those properties public, but I first want to fix this problem).
UserLocation 类包含 3 个带有构造函数和所需的 getter 和 setter 的属性。(我可以公开这些属性,但我首先想解决这个问题)。
public class UserLocation {
private Float latitude;
private Float longitude;
private Date lastActive;
}
JSON body
JSON 正文
Via my AngularJS Ajax call ($http.PUT
) I make a call to the spring controller with the following data:
通过我的 AngularJS Ajax 调用 ( $http.PUT
),我使用以下数据调用 spring 控制器:
{
"latitude": 52.899370,
"longitude": 5.804548,
"lastActive": 1449052628407
}
PostMan request
邮递员请求
I am developing a Cordova application, so to test requests without the need of building my application to my mobile phone, I am using Chrome PostMan Plugin.
我正在开发一个 Cordova 应用程序,所以为了测试请求而不需要将我的应用程序构建到我的手机上,我正在使用Chrome PostMan 插件。
I am making the following call:
我正在拨打以下电话:
URL:
http://server:port/app/location/update/1
URL:
http://server:port/app/location/update/1
Method:
PUT
Method:
PUT
Headers:
Content-Type: application/json
Headers:
Content-Type: application/json
Body:
Body:
{
"latitude": 52.899370,
"longitude": 5.804548,
"timestamp": 1449052628407
}
Request result
请求结果
With the request I get the following result:
通过请求,我得到以下结果:
{"latitude":null,"longitude":null,"lastActive":null}
This means that Spring does make a new instance of my UserLocation
class, but it does not fill it with the data from the body..
这意味着 Spring 确实为我的UserLocation
类创建了一个新实例,但它没有用来自主体的数据填充它。
Spring PUT method
弹簧PUT方法
When using the PUT method in a Spring controller, isn't the entity updated immediately? So that would mean that there would be no extra logic in the controller for updating the entity right? (if the entity is of course a Hibernate/JPAmodel, which can be updated).
在 Spring 控制器中使用 PUT 方法时,实体不是立即更新吗?所以这意味着控制器中不会有额外的逻辑来更新实体,对吗?(如果实体当然是可以更新的Hibernate/JPA模型)。
I cannot seem to figure out the problem. Anyone knows what I am doing wrong?
我似乎无法弄清楚问题所在。有谁知道我做错了什么?
Update
更新
Adding @RequestBody
to my controller code:
添加@RequestBody
到我的控制器代码:
@RequestMapping(value = "/location/update/{id}", method = RequestMethod.PUT)
public UserLocation updateUserLocation(@PathVariable("id") int id, @RequestBody UserLocation user) {
return user;
}
Brings me back to my original question (415 media not supported). Adding this throws a 415 Media not supported error I cannot seem to fix.
让我回到最初的问题(不支持 415 媒体)。添加此会引发 415 Media not supported 错误,我似乎无法修复。
Fixed. Solution below
固定的。下面的解决方案
采纳答案by Driss Amri
I don't see a @RequestBody in your Controller for the UserLocation object? Also make sure your properties have getters and setters.
我在您的控制器中没有看到 UserLocation 对象的 @RequestBody ?还要确保你的属性有 getter 和 setter。
public UserLocation updateUserLocation(@PathVariable("id") int id, UserLocation user) {
When doing a HTTP PUT, you WILL have to put extra logic to persist your object to the database. You will need to call your DAO or Repository to persist your object. Usually you map your incoming UserLocation object to a real JPA/Hibernate entity that you persist. This will not happen automatically.
在执行 HTTP PUT 时,您将不得不放置额外的逻辑来将您的对象持久化到数据库中。你需要调用你的 DAO 或 Repository 来持久化你的对象。通常,您将传入的 UserLocation 对象映射到您保留的真实 JPA/Hibernate 实体。这不会自动发生。
回答by Tom Sebastian
Problem is you missed to annotate the UserLocation parameter with @RequestBody
问题是您错过了注释 UserLocation 参数 @RequestBody
..updateUserLocation(@PathVariable("id") int id, @RequestBody UserLocation user)
Also make sure to generate getters
and setters
for UserLocation
memeber variables.
还要确保生成getters
和setters
为UserLocation
成员变量。