JSON响应对象具有其他名称/值对

时间:2015-09-30 15:39:46

标签: java json rest jersey-1.0

我正在使用REST Post服务(使用Jersey 1.19)来创建新的会话详细信息。

这是我的代码:

@POST
@Path("/createSession")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response createSession(String json) throws JSONException{

        JSONObject jsonObject = new JSONObject(json);
        System.out.println("JSON Object is: " + jsonObject);

        String customerCIF = jsonObject.optString("CIF");
        System.out.println("CIF from request is: " + customerCIF);

        SessionInfo sessionInfo = new SessionInfo(customerCIF);

        return Response.ok(sessionInfo,MediaType.APPLICATION_JSON).build();
}

我的请求正文如下:

{
    "CIF": "786"
}

应返回的sessionInfo对象是:

@XmlRootElement
public class SessionInfo {
    @XmlElement(name="sessionID")
    public static UUID sessionID;
    @XmlElement(name="CIF")
    public String CIF;

    //TODO: Transaction List to be added here
    @XmlElement(name="transactions")
    public String transactions;

    @XmlElement(name="owner")
    public String owner;
    @XmlElement(name="createdBy")
    public String createdBy;
    @XmlElement(name="createdTime")
    public String createdTime;
    @XmlElement(name="status")
    public String status;

    public SessionInfo(String CIF){
        SessionInfo.sessionID = UUID.randomUUID();

        this.CIF = CIF;

        //this.transactions = null;

        this.owner = "owner";
        this.createdBy = "ownerRest";   

        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        this.createdTime = dateFormat.format(new Date());

        this.status = "P";
    }
}

我得到的输出是:

{
CIF: "786"
owner: "owner"
createdBy: "ownerRest"
createdTime: "2015/09/30"
status: "P"
sessionID: "37249cfe-4fdd-4d6c-9a4a-8b868ff2c781"
cif: "786"
}

我不明白为什么在回复的最后我会得到额外的cif:786

任何指针?

此致 阿里。

0 个答案:

没有答案