我的代码是这样的:
@XmlRootElement
@XmlType(propOrder = {"id", "name", "content", "disclaimer", "buttons"})
public class Product {
private String id;
private String name;
private String content;
private String disclaimer;
private List<ProductButton> buttons = new ArrayList<ProductButton>();}
服务代码为:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/GetProduct")
public Response getProduct() {
try {
Product product = generateProductById(productId);
return Response.ok().entity(product).build();
}
catch (Exception e) {
log.error(e.getLocalizedMessage(), e);
return Response.serverError().build();
}
}
但是输出的 json 字符串不是按顺序排列的。
我的预期顺序与我的对象相同,但结果是:“name”、“id”、“content”...
任何人都知道我怎么能做到这一点?
- - - - - - - - - - - - - - - - - - - 更新 - - - - - - ---------------------
@JsonPropertyOrder({"id", "name", "content", "disclaimer", "buttons"})
public class Product {
private String id;
private String name;
private String content;
private String disclaimer;
private List<ProductButton> buttons = new ArrayList<ProductButton>();}