这个问题是针对泽西岛2.x的。
假设我有个好方法。客户端请求包含XML或JSON的接受头。该方法是否可以在客户端请求的表示中返回一个POJO?
目前,我的GET方法使用Jackson返回JSON。但是,我不知道如何使它在客户端请求时返回XML。我在这里看到的所有示例都期望JAXB注释POJO。
返回的POJO没有JAXB注释,我不允许添加它们。但是对象确实实现了JavaBean契约,所以它是一个真正的POJO。
因为Jackson可以将一个POJO (没有注释)序列化为JSON,所以我希望有一个现有的MessageBodyWriter,它对XML也有同样的作用。
如果这是可能的:-我如何注释GET方法?目前,我使用下面的行。够了吗?
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })谢谢你的帮助。
发布于 2014-02-25 10:10:29
是的,这在这里是可能的--一个小例子:
@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;
}}
菜谱的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中请求URL
http://test:8080/YourService/rest/recipeChrome请求头是:
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) 在Advanced客户机中,请求标头是:
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不同之处是请求头接受。浏览器将其设置为接受: text/html、application/xhtml+xml、application/xml
REST客户端将其设置为接受:/
因此,有了这个参数,如果xou接收JSON或XML作为响应,您就会产生影响。
https://stackoverflow.com/questions/22009203
复制相似问题