簡體   English   中英

Java REST服務接受POJO,但字段始終為null

[英]Java REST service accepts POJO, but fields are always null

我有一個使用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}

我已經遍歷了您的問題中包含的所有代碼,並且我有一些建議:

TerminateActorCommand POJO中,向與您的JSON屬性匹配的成員添加@JsonProperty批注(存在屬性訪問器方法但不包含mutator方法可能會使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反序列化時是否正在調用該構造函數。

cURL命令-d有效負載規范中的第一和第二個屬性之間也有不必要的空間,這應該不會引起問題,但是刪除該空間將排除可能的問題。

我認為未設置屬性是因為您的字段/設置器未標記為@JsonProperty 即使您已在參數化構造函數中將其標記為json屬性,但使用批注標記這些字段或設置器也將有所幫助,因為您的庫/框架可能正在使用no-arg構造函數來實例化該對象,然后對創建的對象進行延遲設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM