Java REST服务接受POJO,但字段始终为null [英] Java REST service accepts POJO, but fields are always null

查看:168
本文介绍了Java REST服务接受POJO,但字段始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用POJO的REST服务.方法如下:

I have a REST service that uses a POJO. 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;
}

这是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 + '}';
    }
}

当我用CURL调用时:

When I call this with 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

我得到了返回值并看到了打印语句,但是TerminationCommand的字段全为空.我得到了一个实例化的对象,但是我发送的JSON并未填充在该对象上.

I get the return value and see the print statement,but the TerminationCommand's fields are all null. I get an instantiated object but the JSON I'm sending does not get populated on the object.

为什么?

这是输出:

信息:正在运行终止:TerminateActorCommand {actorEid = null,terminatorEid = null,reason = null,effectiveTerminationDate = null}

Info: Running terminate: 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:

TerminateActorCommand POJO中,向与您的JSON属性匹配的成员添加@JsonProperty批注(存在属性访问器方法但不包含mutator方法可能会使Jackson感到困惑):

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):


@JsonProperty String actorEid;
@JsonProperty String terminatorEid;
@JsonProperty String reason;

如果添加@JsonProperty不能解决您的问题,请检查TerminateActorCommand类中当前定义的no-arg构造函数.当您使用Jackson @JsonCreator批注时,无需定义无参数构造函数,但是如果杰克逊在反序列化过程中找不到良好的匹配项,它将退回到使用无参数构造函数.我的猜测是,在JSON反序列化过程中当前正在调用no-arg构造函数(因此是null属性值),所以我建议删除该构造函数,或者如果需要的话(可能在代码的其他部分) ),在no-arg构造函数中添加System.out.println,这样您就可以确定在Jackson执行JSON反序列化时是否正在调用该构造函数.

If adding the @JsonProperty doesn't resolve your issue, examine the no-arg constructor that is currently defined within your TerminateActorCommand class. 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. 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.

cURL命令-d有效负载规范中,第一个属性和第二个属性之间也有不必要的空间,这应该不会引起问题,但是删除该空间将排除可能的问题.

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.

这篇关于Java REST服务接受POJO,但字段始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆