【问题标题】:Jackson Java EE WS - XML is empty but JSON has all fieldsJackson Java EE WS - XML 为空,但 JSON 包含所有字段
【发布时间】:2016-07-06 23:44:39
【问题描述】:

我有一个使用 REST 服务公开的 POJO:

@XmlRootElement
public class Field implements Serializable{
    /**
     * 
     */
    @JsonIgnore
    private static final long serialVersionUID = 1L;

    FieldType fieldType;

    String name;

    Object deafultValue;

    Map<String, String> choices;

    boolean required;

    public Field(){}

    @JsonCreator
    public Field(@JsonProperty("fieldType") FieldType fieldType, String name, Object deafultValue, Map<String, String> choices, boolean required) {
        super();
        this.fieldType = fieldType;
        this.name = name;
        this.deafultValue = deafultValue;
        this.choices = choices;
        this.required = required;
    }

    @JsonProperty 
    public FieldType getFieldType() {
        return fieldType;
    }

    @Override
    public String toString() {
        return "Field [fieldType=" + fieldType + ", name=" + name + ", deafultValue=" + deafultValue + ", choices="
                + choices + ", required=" + required + "]";
    }

    public String getName() {
        return name;
    }

    public Object getDeafultValue() {
        return deafultValue;
    }

    public Map<String, String> getChoices() {
        return choices;
    }

    public boolean isRequired() {
        return required;
    }

}

我有许多暴露的对象,但其中大多数都标有@Entity 注释并且它们工作正常。

这是我的网络服务代码:

@GET
    @Path("search-fields")
    @Produces({"application/xml", "application/json"})
    public List<Field> getSearchFields(){
        L.debug("Getting Actor search fields: {}", new Actor().getSearchFields());
        return new Actor().getSearchFields();
    }

日志消息说数据在那里:

DEBUG ActorsFacadeREST - 获取 Actor 搜索字段:[Field [fieldType=INPUT, name=Name, deafultValue=null, choice=null, required=true]]

当我查询 JSON 时:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://localhost:8080/actor-service/webresources/entities.actors/search-fields/ && echo ""
HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/10
Content-Type: application/json
Content-Length: 88
Date: Sun, 20 Mar 2016 13:44:46 GMT

[{"fieldType":"INPUT","name":"Name","deafultValue":null,"choices":null,"required":true}]

当我要求 XML 时:

$ curl -i -H "Accept: application/xml" -H "Content-Type: application/json" -X GET http://localhost:8080/actor-service/webresources/entities.actors/search-fields/ && echo ""
HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Undertow/1
Server: WildFly/10
Content-Type: application/xml
Content-Length: 88
Date: Sun, 20 Mar 2016 13:46:47 GMT

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><collection><field/></collection>

为什么我的 JSON 有对象数据,但 XML 是空的?除了@XmlRootElement 之外,是否还有其他我应该使用的 XML 注释(我找不到)?

编辑添加

我有以下可以正常工作的 bean:

public class Actor implements Serializable, HasImage, Searchable<Actor> {
    private static final transient Logger L = LoggerFactory.getLogger(Actor.class);
    private static final long serialVersionUID = 1L;

    private Long id;

    private String eid;

    private String name;

    private String status;

    private String reportsToEid;

    private String email;

    String imageLocation;


    PathType pathType;

    public Actor() {
        L.debug("Actor created with default constructor");
    }

    @JsonCreator
    public Actor(@JsonProperty("id") Long id, @JsonProperty("eid") String eid, @JsonProperty("name")String name, 
            @JsonProperty("status") String status, @JsonProperty("reportsToId") String reportsToEid, 
            @JsonProperty("email") String email, @JsonProperty("imageLocation")String imageLocation, 
            @JsonProperty("pathType") PathType pathType) {
        super();
        L.debug("Actor created with JsonCreator marked constructor");
        this.id = id;
        this.eid = eid;
        this.name = name;
        this.status = status;
        this.reportsToEid = reportsToEid;
        this.email = email;
        this.imageLocation = imageLocation;
        this.pathType = pathType;
    }

【问题讨论】:

    标签: java json xml web-services


    【解决方案1】:

    我认为您还必须使用 @XmlElement 注释您的字段...

    【讨论】:

    • 好的,我发布了一个工作对象(在我上面的编辑中)。为什么它不需要它们?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    • 2015-01-11
    • 2011-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多