我有一个POJO,我正在使用REST服务公开:
@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
注释,并且它们工作正常。
这是我的 Web 服务代码:
@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();
}
日志消息指出数据在那里:
调试参与者外观休息 - 获取执行组件搜索字段:[字段 [字段类型=输入,名称=名称,deafultValue=null,选择=空,必需=真]]
当我查询 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注释(我找不到)?
已编辑以添加
我有以下工作正常的豆子:
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;
}
我认为您还必须用@XmlElement注释您的字段...