没有根元素名称resteasy响应

时间:2013-10-06 18:49:11

标签: json hibernate resteasy

我在使用指定对象的根名称返回对象列表时遇到了一些麻烦。我尝试过几种不同的东西。我相信我一如既往地做着令人难以置信的愚蠢事情,我感谢任何帮助。

这是我的对象

@XmlRootElement
@JsonRootName(value = "Bixasset")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@Produces("application/json")
@Indexed    
public class Bixasset implements java.io.Serializable {
    @Id
    private UUID id;

    private Client client;

    private String name;

    private String asseturl;
    private Character active;
    private Integer width;
    private Integer height;
    private String thumbnailurl;
    private Date createddate;
    private String filetype;
    private String category;


    public Bixasset() {
    }

    public Bixasset(UUID id) {
        this.id = id;
    }

    public Bixasset(UUID id, Client client, String name, String asseturl,Character active, Integer width, Integer height, String thumbnailurl
        ,Date createddate, String filetype, String category) {
        this.id = id;
        this.client = client;
        this.name = name;
        this.asseturl = asseturl;
        this.active = active;
        this.width = width;
        this.height = height;
        this.thumbnailurl = thumbnailurl;
        this.createddate = createddate;
        this.filetype = filetype;
        this.category = category;


    }



    public UUID getId() {
        return this.id;
    }

    public void setId(UUID id) {
        this.id = id;
    }
    @ManyToOne(fetch=FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @JoinColumn(name="clientid")
    public Client getClient() {
        return this.client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAsseturl() {
        return asseturl;
    }

    public void setAsseturl(String asseturl) {
        this.asseturl = asseturl;
    }

    public Character getActive() {
        return active;
    }

    public void setActive(Character active) {
        this.active = active;
    }

    public Integer getWidth() {
        return width;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getHeight() {
        return height;
    }

    public void setHeight(Integer height) {
        this.height = height;
    }

    public String getThumbnailurl() {
        return thumbnailurl;
    }

    public void setThumbnailurl(String thumbnailurl) {
        this.thumbnailurl = thumbnailurl;
    }

    public Date getCreateddate() {
        return createddate;
    }

    public void setCreateddate(Date createddate) {
        this.createddate = createddate;
    }

    public String getFiletype() {
        return filetype;
    }

    public void setFiletype(String filetype) {
        this.filetype = filetype;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    }

这是我的方法

@Path("/bixasset/")
@Produces(MediaType.APPLICATION_JSON)
public Response queryBixAsset(@QueryParam("id") String id, @QueryParam("filetype") String filetype, @QueryParam("category") String category,   @QueryParam("token") String token) {
    try{

        List<Bixasset> results = new ArrayList<Bixasset>(); 


        UUID ClientId = dAL.validateToken(token);

        if(token != null && ClientId != null){


                String query ="FROM Bixasset WHERE client.id = '" + ClientId + "' "; 


                if(id != null && !id.equals(""))
                    query += " AND id = '" + id + "'";

                if(filetype != null && !filetype.equals(""))
                    query += " AND filetype.id = '" + filetype + "'";

                if(category != null && !category.equals(""))
                    query += " AND category = '" + category + "'";




                query+= "  LIMIT 1000";
                results = dAL.query(query);
                System.out.println("query size" + results.size());


        }else{
            return Response.status(401).build();
        }


        return Response.ok(results).build();
    }catch(Exception e){
        return Response.status(400).build();
    }
}

这是我的结果

   [
  {
    "id": "99f516a2-f7ef-4bc9-a627-73981a2fc3ae",
    "client": {
      "id": "388e16d6-d35e-4f8c-bba5-b1147b824473",
      "reseller": null,
      "name": "Phizzle",
      "createddate": 1357554435574,
      "address1": "123",
      "address2": "",
      "city": "San Francisco",
      "state": "CA",
      "postalcode": "80130",
      "country": "USA",
      "description": "Phizzle Master Client"
    },
    "name": "gears_animated.gif",
    "asseturl": "https://s3.amazonaws.com/asdad/99f516a2-f7ef-4bc9-a627-73981a2fc3ae.gif",
    "active": "0",
    "width": 141,
    "height": 141,
    "thumbnailurl": "https://s3.amazonaws.com/dadadsda/99f516a2-f7ef-4bc9-a627-73981a2fc3ae__thumbnail.jpg",
    "createddate": 1380927929287,
    "filetype": "image/gif",
    "category": "Images"
  }
]

希望我的结果看起来像

   {"Bixasset" [
  {
    "id": "99f516a2-f7ef-4bc9-a627-73981a2fc3ae",
    "client": {
      "id": "388e16d6-d35e-4f8c-bba5-b1147b824473",
      "reseller": null,
      "name": "Phizzle",
      "createddate": 1357554435574,
      "address1": "123",
      "address2": "",
      "city": "San Francisco",
      "state": "CA",
      "postalcode": "80130",
      "country": "USA",
      "description": "Phizzle Master Client"
    },
    "name": "gears_animated.gif",
    "asseturl": "https://s3.amazonaws.com/adsdadd/99f516a2-f7ef-4bc9-a627-73981a2fc3ae.gif",
    "active": "0",
    "width": 141,
    "height": 141,
    "thumbnailurl": "https://s3.amazonaws.com/adadsd/99f516a2-f7ef-4bc9-a627-73981a2fc3ae__thumbnail.jpg",
    "createddate": 1380927929287,
    "filetype": "image/gif",
    "category": "Images"
  }
] }

1 个答案:

答案 0 :(得分:0)

您是否尝试过设置@XmlRootElement的Annotation变量而不是使用@JsonRootName? 我的意思是:

@XmlRootElement(name = "Bixasset")