[英]Java REST service accepts POJO, but fields are always null
I have a REST service that uses a POJO. 我有一个使用POJO的REST服务。 Here is the method:
方法如下:
@POST
@Path("terminate")
@Produces({"application/xml", "application/json"})
@Consumes({"application/xml", "application/json"})
public TerminateActorCommand terminateActor(TerminateActorCommand cmd) {
System.out.println("Running terminate: " + cmd);
Query query = em.createNamedQuery("Actor.terminate");
query.setParameter("eid", cmd.getActorEid());
query.executeUpdate();
return cmd;
}
Here is the POJO 这是POJO
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author mike
*/
@XmlRootElement
public class TerminateActorCommand {
String actorEid;
String terminatorEid;
String reason;
Date effectiveTerminationDate;
public TerminateActorCommand() {
}
@JsonCreator
public TerminateActorCommand(@JsonProperty("actorEid") String actorEid, @JsonProperty("terminatorEid") String terminatorEid,
@JsonProperty("reason") String reason) { //, @JsonProperty("effectiveTerminationDate") Date effectiveTerminationDate) {
this.actorEid = actorEid;
this.terminatorEid = terminatorEid;
this.reason = reason;
//this.effectiveTerminationDate = effectiveTerminationDate;
}
public CommandType getCommandType() {
return CommandType.TERMINATE_ACTOR;
}
public String getActorEid() {
return actorEid;
}
public String getTerminatorEid() {
return terminatorEid;
}
public String getReason() {
return reason;
}
public Date getEffectiveTerminationDate() {
return effectiveTerminationDate;
}
@Override
public String toString() {
return "TerminateActorCommand{" + "actorEid=" + actorEid + ", terminatorEid=" + terminatorEid + ", reason=" + reason + ", effectiveTerminationDate=" + effectiveTerminationDate + '}';
}
}
When I call this with CURL: 当我用CURL调用时:
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"actorEid":"mb995a", "terminatorEid":"mb995a","reason":"testing"}' http://127.0.0.1:8080/actor-service/webresources/net.mikeski.auth.entities.actors/terminate
I get the return value and see the print statement,but the TerminationCommand's fields are all null. 我得到了返回值并看到了打印语句,但是TerminationCommand的字段全为空。 I get an instantiated object but the JSON I'm sending does not get populated on the object.
我得到了一个实例化的对象,但是我发送的JSON并未填充在该对象上。
Why??? 为什么???
Here's the output: 这是输出:
Info: Running terminate: TerminateActorCommand{actorEid=null, terminatorEid=null, reason=null, effectiveTerminationDate=null}
信息:运行终止:TerminateActorCommand {actorEid = null,terminatorEid = null,reason = null,effectiveTerminationDate = null}
I've walked through all of the code included in your question and I have some suggestions: 我已经遍历了您的问题中包含的所有代码,并且我有一些建议:
Within the TerminateActorCommand
POJO, add the @JsonProperty
annotation to the members that match your JSON properties (the presence of the property accessor methods but absence of mutator methods may be confusing Jackson): 在
TerminateActorCommand
POJO中,向与您的JSON属性匹配的成员添加@JsonProperty
批注(存在属性访问器方法但不包含mutator方法可能会使Jackson感到困惑):
@JsonProperty String actorEid;
@JsonProperty String terminatorEid;
@JsonProperty String reason;
If adding the @JsonProperty
doesn't resolve your issue, examine the no-arg constructor that is currently defined within your TerminateActorCommand
class. 如果添加
@JsonProperty
不能解决您的问题,请检查TerminateActorCommand
类中当前定义的no-arg构造函数。 When you use the Jackson @JsonCreator
annotation, there is no need to define a no-arg constructor, but if Jackson is unable to find a good match during deserialization, it will fallback to using a no-arg constructor. 当您使用Jackson
@JsonCreator
批注时,无需定义无参数构造函数,但是,如果杰克逊在反序列化过程中找不到良好的匹配项,它将退回到使用无参数构造函数。 My guess is that the no-arg constructor is what is currently being called during JSON deserialization (thus the null
property values), so I would suggest either removing that constructor or, if it is needed (maybe in other parts of your code), add a System.out.println
within the no-arg constructor, so you will know for certain if that constructor is being called when Jackson performs the JSON deserialization. 我的猜测是,在JSON反序列化过程中当前正在调用no-arg构造函数(因此为
null
属性值),因此我建议删除该构造函数,或者如果需要的话(可能在代码的其他部分),在no-arg构造函数中添加System.out.println
,这样您就可以确定在Jackson执行JSON反序列化时是否正在调用该构造函数。
There is also an unnecessary space between the first and second properties in your cURL
command -d
payload specification and that shouldn't cause a problem, but removing that space will rule that out as a possible problem. 在
cURL
命令-d
有效负载规范中的第一和第二个属性之间也有不必要的空间,这应该不会引起问题,但是删除该空间将排除可能的问题。
I think the properties are not set because your fields/setters are not marked as @JsonProperty
. 我认为未设置属性是因为您的字段/设置器未标记为
@JsonProperty
。 Even though you have marked those as json properties in the parameterized constructor, marking these fields or setters with annotations should help because your library/framework might be using no-arg constructor to instantiate the object and then set the properties lazily on the created object. 即使您已在参数化构造函数中将其标记为json属性,但使用批注标记这些字段或设置器也将有所帮助,因为您的库/框架可能正在使用no-arg构造函数来实例化该对象,然后对创建的对象进行延迟设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.