【问题标题】:Getting json as a pojo将 json 作为 pojo
【发布时间】:2015-08-09 17:52:59
【问题描述】:

这是我的对象

public class ServiceGroup {

    private String ParticipantIdentifierScheme;
    private String ParticipantIdentifierValue;

    public void setParticipantIdentifierScheme(String ParticipantIdentifierScheme) {
        this.ParticipantIdentifierScheme = ParticipantIdentifierScheme;
    }

    public String getParticipantIdentifierScheme() {
        return ParticipantIdentifierScheme;
    }

    public void setParticipantIdentifierValue(String ParticipantIdentifierValue) {
        this.ParticipantIdentifierValue = ParticipantIdentifierValue;
    }

    public String getParticipantIdentifierValue() {
        return ParticipantIdentifierValue;
    }

这样处理请求

@Path("/participants")
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response addUserToSMP(@HeaderParam("authorization") String authString, ServiceGroup sg) {
    return Response.ok().entity(sg).build();
}

我已经放了那个小响应,以便查看到底解析了什么。

我的输入应该是这样的

{
  "ParticipantIdentifierScheme": "scheme",
  "ParticipantIdentifierValue": "value"
}

但是输出如下

{
  "participantIdentifierScheme": null,
  "participantIdentifierValue": null
}

我真正想要的是以某种方式检查输入是否格式正确,并将它们保存在 ServiceGroup 对象中以供进一步使用

更新

将变量设置为公共返回以下内容

{
  "ParticipantIdentifierScheme": "scheme",
  "participantIdentifierScheme": "scheme",
  "ParticipantIdentifierValue": "value",
  "participantIdentifierValue": "value",
 }

更新 2

@XmlRootElement
public class ServiceGroup {

    @XmlElement(name = "ParticipantIdentifierScheme")
    private String ParticipantIdentifierScheme;

    @XmlElement(name = "ParticipantIdentifierValue")
    private String ParticipantIdentifierValue;

我得到了

{
  "ParticipantIdentifierScheme": "scheme",
  "participantIdentifierScheme": "scheme",
  "ParticipantIdentifierValue": "value",
  "participantIdentifierValue": "value"
}

【问题讨论】:

  • 以某种方式注释ServiceGroup sg(可能是@RequestBody)。为类ServiceGroup 和字段@XmlElement(name = "ParticipantIdentifierScheme") 添加注释@XmlRootElement
  • @glw 请检查我的更新
  • 只是为了测试将ParticipantIdentifierScheme更改为participantIdentifierScheme

标签: java json jersey pojo


【解决方案1】:

问题在于序列化还会查看您的 getter 方法。您应该只将 getter 方法注释为元素。它看到了 4 个不同的值,因为您将大写变量名称作为私有变量,而 getter 将被视为小写。您还可以使用 @XmlAccessorType 指定您只想序列化公共成员。

看这里

http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

将您的变量更改为小写并仅注释您的 getter 方法。不要注释 setter 方法。它会抱怨,因为它是多余的。

@XmlRootElement
public class ServiceGroup {

    private String participantIdentifierScheme;
    private String participantIdentifierValue;

    public void setParticipantIdentifierScheme(String ParticipantIdentifierScheme) {
        this.ParticipantIdentifierScheme = ParticipantIdentifierScheme;
    }

    @XmlElement(name = "ParticipantIdentifierScheme")
    public String getParticipantIdentifierScheme() {
        return ParticipantIdentifierScheme;
    }

    public void setParticipantIdentifierValue(String ParticipantIdentifierValue) {
        this.ParticipantIdentifierValue = ParticipantIdentifierValue;
    }

    @XmlElement(name="ParticipantIdentifierValue")
    public String getParticipantIdentifierValue() {
        return ParticipantIdentifierValue;
    }

【讨论】:

    猜你喜欢
    • 2021-09-03
    • 2015-09-10
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多