【问题标题】:Java REST service accepts POJO, but fields are always nullJava REST 服务接受 POJO,但字段始终为空
【发布时间】:2016-03-08 02:35:09
【问题描述】:

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

@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 调用它时:

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 没有填充到该对象上。

为什么???

这是输出:

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

【问题讨论】:

    标签: java json rest


    【解决方案1】:

    我已经浏览了您问题中包含的所有代码,我有一些建议:

    在TerminateActorCommand POJO 中,将@JsonProperty 注释添加到与您的JSON 属性匹配的成员(属性访问器方法的存在但缺少突变器方法可能会使Jackson 感到困惑):

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

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

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

    【讨论】:

    • 嗯,这很有趣。正如预期的那样,我添加了您的注释,没有区别。我添加了设置器,并且该东西与无参数构造函数一起使用。当我像原始代码一样添加@JsonCreator 构造函数并摆脱其他构造函数时,我得到MessageBodyReader not found for media type=application/json。我希望这个对象是不可变的。我该怎么做?
    • 构造函数上的@JsonCreator 注释,没有setter 方法,是保持不变性的正确方法。您使用的是什么版本的 Jackson(jackson-core、jackson-annotations 和 jackson-databind)?您的项目是否包含单独的 Jackson 配置文件?
    • 这是正确的,我确实不需要需要摆脱默认构造函数,只是创建了一个带有我想要设置的字段的@JsonCreator标记的构造函数并且它可以工作。跨度>
    【解决方案2】:

    我认为属性未设置,因为您的字段/设置器未标记为@JsonProperty。即使您在参数化构造函数中将它们标记为 json 属性,使用注释标记这些字段或设置器应该会有所帮助,因为您的库/框架可能使用无参数构造函数来实例化对象,然后在创建的对象上懒惰地设置属性。

    【讨论】:

    • 我希望对象是不可变的。带注释的属性/设置器有效,但它不是不可变的。
    • @mikeb 我同意不变性。就像在黑暗中拍摄一样,您能否尝试添加编译器标志 javac -d:vars 并查看是否有效。如果您使用的是 java 8,请尝试使用 javac -parameters
    猜你喜欢
    • 2016-06-15
    • 2016-09-29
    • 1970-01-01
    • 2015-12-26
    • 2021-07-18
    • 2018-05-05
    • 2017-03-15
    • 1970-01-01
    • 2011-08-28
    相关资源
    最近更新 更多