首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >泽西岛可以根据请求的接受头将GET返回的POJO转换为XML或JSON吗?

问泽西岛可以根据请求的接受头将GET返回的POJO转换为XML或JSON吗?
EN

Stack Overflow用户
提问于 2014-02-25 09:01:30
回答 1查看 2.8K关注 0票数 1

这个问题是针对泽西岛2.x的。

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

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

返回的POJO没有JAXB注释,我不允许添加它们。但是对象确实实现了JavaBean契约,所以它是一个真正的POJO。

因为Jackson可以将一个POJO (没有注释)序列化为JSON,所以我希望有一个现有的MessageBodyWriter,它对XML也有同样的作用。

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

代码语言:javascript
复制
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML })
  • 我正在通过web.xml和Maven配置泽西。我已经实现了JSON序列化,但我还没有弄清楚如何配置XML输出。需要什么配置才能使XML工作?

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-25 10:10:29

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

代码语言:javascript
复制
@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:

代码语言:javascript
复制
@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

代码语言:javascript
复制
 http://test:8080/YourService/rest/recipe

Chrome请求头是:

代码语言:javascript
复制
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客户机中,请求标头是:

代码语言:javascript
复制
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作为响应,您就会产生影响。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22009203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档