JsonPropertyOrder在javax rs响应中不起作用

huangapple 未分类评论43阅读模式
英文:

JsonPropertyOrder doesnt work with javax rs response

问题

我正在尝试使用JsonPropertyOrder注解对响应的JSON进行元素排序,但是在响应的JSON中,属性并没有被排序。我尝试在JSON属性注解中保留索引,但也没有帮助。我将对象传递给javax.ws.rs.core.ResponseBuilder.entity。请有人能帮忙解决一下吗?

REST服务的响应类似于以下内容:

@GET
@Path("/test/output")
public Response getOffers() {
    return Response.status(200)
            .entity(new ExtInstallResponse("testId", "testVersion", "testName"))
            .type(MediaType.APPLICATION_JSON).build();
}

POJO类如下:

@JsonPropertyOrder({"name", "id", "version"})
public class ExtInstallResponse {

    @JsonProperty("id")
    private String id;

    @JsonProperty("version")
    private String version;

    @JsonProperty("name")
    private String name;

    // 构造方法以及其他方法...

}

我得到的响应格式如下,但我希望它按照name、id、version的顺序排序:

{
    "id": "ExtensionId3",
    "name": "Extension Id 3",
    "version": "1.0"
}

当我从Java的主方法中尝试时,同样的代码可以正常工作。

英文:

I am trying to order elements for response json using JsonPropertyOrder annotation but Json property is not been ordered in response json. I tried keeping index in json property annotation that also didnt help . I am passing the object to javax.ws.rs.core.ResponseBuilder.entity. Could some one please help on this

rest service response some thing like below

 @GET
    
    @Path("/test/output")
    
    public Response getOffers()
    {
        
          return Response.status(200)
.entity(new ExtInstallResponse("testId","testVersion","testName"))
.type(MediaType.APPLICATION_JSON).build();
    
    }

POJO as below :

   @JsonPropertyOrder({"name", "id", "version"})
    public class ExtInstallResponse {
    
      @JsonProperty("id")
      private String id;
    
      @JsonProperty("version")
      private String version;
    
      @JsonProperty("name")
      private String name;
    
      /**
       * constructor.
       *
       * @param id id
       * @param version version
       * @param name name
       */
      public ExtInstallResponse(String id, String version, String name) {
        this.id = id;
        this.version = version;
        this.name = name;
      }
    
      public String getId() {
        return id;
      }
    
      public String getName() {
        return name;
      }
    
      public void setId(String id) {
        this.id = id;
      }
    
      public void setName(String name) {
        this.name = name;
      }
    
      public String getVersion() {
        return version;
      }
    
      public void setVersion(String version) {
        this.version = version;
      }
    }

I am getting response in below format but I need it to be orderd as name, id,version

{
    "id": "ExtensionId3",
    "name": "Extension Id 3",
    "version": "1.0"
}

same works fine when I try from java main method

huangapple
  • 本文由 发表于 2020年4月9日 19:11:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/61119816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定