我有一个REST服务,可以输出POJO列表。响应XML / JSON包含使用XMLElement注释的所有字段。有没有办法限制响应中的字段(以编程方式运行时)?我还可以在运行时再次指定字段的顺序吗?
POJO:
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Employee {
@XmlElement
@Column(name="Name", length=75) @NotNull @Length(max=75)
private String name;
@XmlElement
@Column(name="designation", length=75)
private String designation;
@XmlElement
@Column(name="department", length=75)
private String department;
}
@Path("employee")
public class EmployeeRestService {
@GET
@Path("json")
@Produces(MediaType.APPLICATION_JSON)
public Response emp() {
return Response.ok(getDetails(), MediaType.APPLICATION_JSON).build();;
}
@Path("xml")
@Produces(MediaType.APPLICATION_XML)
public Response emp() {
GenericEntity<List<Employee>> list = new GenericEntity<List<Employee>>(getDetails()) {};
return Response.ok(list, MediaType.APPLICATION_XML).build();;
}
public List<Employee> getDetails() {
.....
return list;
}
}
My output now is:
JSON:
[{name:Tom,designation:Manager,department:IT},{name:Jim,designation:Clerk,department:IT}]
XML:
<employees>
<employee>
<name>Tom</name>
<designation>Manager</designation>
<department>IT</department>
</employee>
<employee>
<name>Jim</name>
<designation>Clerk</designation>
<department>IT</department>
</employee>
</employees>
Desired output:
JSON:
[{designation:Manager,name:Tom},{designation:Clerk,name:Jim}]
XML:
<employees>
<employee>
<designation>Manager</designation>
<name>Tom</name>
</employee>
<employee>
<designation>Clerk</designation>
<name>Jim</name>
</employee>
</employees>
仅在提交请求时才知道字段和订单。所以像JSONIgnore这样的注释对我没用。 我该怎么做?我试过Genson。虽然我可以限制JSON响应中的字段,但我无法对XML响应进行限制。此外,我无法订购字段。
答案 0 :(得分:0)
要设置自定义订单,您可以使用:
@XmlType(propOrder = {&#34; names&#34;,&#34; name&#34;,&#34; department&#34;,&#34; abcd&#34;})