是否可以根据请求的Accept标头将GET返回的POJO转换为XML或JSON?

时间:2014-02-25 09:01:31

标签: java xml json rest jersey

这个问题适用于Jersey 2.x。

假设我有一个GET方法。客户端请求包含XML或JSON的Accept标头。该方法是否有可能在客户端请求的表示中返回POJO?

目前,我的GET方法使用Jackson返回JSON。但我不知道如何在客户端请求它时返回XML。我在这里看到的所有例子都希望POJO被JAXB注释。

返回的POJO没有JAXB注释,我不允许添加它们。但是该对象确实符合JavaBean契约,因此它是一个真正的POJO。

由于Jackson可以将POJO(没有注释)序列化为JSON,我希望现有的MessageBodyWriter能够为XML做同样的事情。

如果可行: - 如何注释GET方法?目前,我使用下面的行。这够了吗?

@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })
  • 我正在通过web.xml和Maven配置Jersey。我有JSON序列化工作,但我还没有想出如何配置XML输出。使XML工作需要什么配置?

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

是的,这可能是一个小例子:

@Path("/recipe")
public class RecipeResource {
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public List<Recipe> getRecipesBrowser() {
    System.out.println("REST Service Method getRecipesBrowser called");
    System.out.println("Called URI: " + uriInfo.getAbsolutePath());
    List<Recipe> dummyData = new ArrayList<>();
    dummyData.add(new Recipe(new Long(1), "Recipe1", "Description1", null));
    dummyData.add(new Recipe(new Long(2), "Recipe2", "Description2", null));
    dummyData.add(new Recipe(new Long(3), "Recipe3", "Description3", null));
    dummyData.add(new Recipe(new Long(4), "Recipe4", "Description4", null));
    dummyData.add(new Recipe(new Long(5), "Recipe5", "Description5", null));
    dummyData.add(new Recipe(new Long(6), "Recipe6", "Description6", null));
    dummyData.add(new Recipe(new Long(7), "Recipe7", "Description7", null));
    dummyData.add(new Recipe(new Long(8), "Recipe8", "Description8", null));
    dummyData.add(new Recipe(new Long(9), "Recipe9", "Description9", null));

    return dummyData;
}

}

Recipe的POJO需要注释@XMlRootElement:

@XmlRootElement
public class Recipe {

private Long recipeId;

private String name;

private String description;

private List<Fixing> fixings;

public Recipe() {

}

public Recipe(Long id, String name, String description, List<Fixing> fixings) {
    super();
    this.recipeId = id;
    this.name = name;
    this.description = description;
    this.fixings = fixings;
}

public Long getRecipeId() {
    return recipeId;
}

public void setRecipeId(Long id) {
    this.recipeId = id;
}

public String getName() {
    return name;
}

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

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public List<Fixing> getFixings() {
    return fixings;
}

public void setFixings(List<Fixing> fixings) {
    this.fixings = fixings;
}

}

然后,您将进入浏览器XML并使用REST客户端JSON:

在Chrome中请求网址

 http://test:8080/YourService/rest/recipe

Chrome请求标题为:

GET /YourService/rest/recipe HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)        

在高级REST客户端中,请求标头为:

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/33.0.1750.117 Safari/537.36
Content-Type: text/plain; charset=utf-8 
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: beaker.session.id=f1f5577e0dd2047968a2ada05acc1952; nas_lang=ENG

区别在于Request Header Accept。浏览器将其设置为 接受:text / html,application / xhtml + xml,application / xml

REST客户端将其设置为 接受: /

因此,使用此参数可以影响xou是否接收JSON或XML作为响应。