具有相同 REST GET 的多种响应类型? [英] Multiple response types with same REST GET?

查看:34
本文介绍了具有相同 REST GET 的多种响应类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以返回 JSON 或 XML 的 REST 服务.我在请求中设置什么请求参数来请求某种 MIME 类型?我知道如何在响应中设置它,但必须有一种方法来请求某个特定的.目前我在 URL 中执行此操作

I want to create a REST service that can return either JSON or XML. What request parameter do I set in the request to request a certain mime type? I know how to set it in the response, but there must be a way to request a certain one. Currently I do it in the URL

restServlet/engine/2WS2345

restServlet/engine/2WS2345

jsonServlet/engine/2WS2345

jsonServlet/engine/2WS2345

这让我得到 json 或 xml.但我想我读到有一个参数需要在请求中设置.我正在使用 JAVA...

This gets me json or xml. But I thought I read that there is a parameter to set in the request. I am using JAVA...

推荐答案

如果您使用 jersey,您可以使用 @Produces 注释轻松配置该方法.@Produces({"application/xml", "application/json"})

If you are using jersey you can easily configure the method with @Produces annotation. @Produces({"application/xml", "application/json"})

好消息是您仍然可以将 JAXB 对象作为返回类型.它将自动更改为所需的格式.除非在 Accept Header 中指定 MIME 类型,否则在上述情况下它将始终发送 xml.

Good thing is you can still have a JAXB object as a return type. It will automatically be changed to the required format. Unless MIME type is specified in the Accept Header it will always send xml in the above case.

参考 http://jersey.java.net/nonav/documentation/1.6/user-guide.html

这篇关于具有相同 REST GET 的多种响应类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆