泽西岛多种农产品 [英] Jersey Multiple Produces

查看:89
本文介绍了泽西岛多种农产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在此处进行指导.如何产生多个mime输出.在他们的网站上,这是推荐的方式:

I am following the jersey tutorial here to figure out how one would produce multiple mime outputs. From their website, this is the recommended way:

@GET
@Produces({"application/xml", "application/json"})
    public String doGetAsXmlOrJson() {
    ...
}

我无法弄清楚如何将@Produces抽象化,以便我的代码更欢迎使用它可以产生的其他mime类型.举例来说,我有500种方法都带有此注释:

What I cannot figure out is how to abstract the @Produces away, so that my code is more welcoming to additional mime types it can produce. Say for example I have 500 methods that all have this annotation:

@Produces({"application/xml", "application/json"})

如果我要求将kml添加为mime类型,那么编辑和替换所有这些值肯定会很耗时.

If I get a requirement to add kml as a mime type, editing and replacing all of those values would certainly be time consuming.

@Produces({"application/xml", "application/json", "application/kml"})

是否有可能更有效地设计@Produces,这样我就不会遇到这个问题了?

Is it possible to architect @Produces more efficiently so that I do not have this issue down the road?

推荐答案

了解@Produces批注

@Produces 注释是用于指定资源可以产生并发送回客户端的表示形式的MIME媒体类型.

Understanding the @Produces annotation

The @Produces annotation is used to specify the MIME media types of representations a resource can produce and send back to the client.

JAX-RS运行时比较 Accept 的值传入请求的标头,其值为 @Produces 注释以匹配将处理此类请求的资源方法.

The JAX-RS runtime compares value of the Accept header of an incoming request with the value of the @Produces annotation to match the resource method that will handle such request.

缺少 @Produces 注释,假定支持任何媒体类型(*/*).有关完整的参考,请检查 JAX-RS规范.

In the absence of the @Produces annotation, support for any media type (*/*) is assumed. For a complete reference, check the JAX-RS specification.

要减少 @Produces 代码中的注释,您可以注释资源类而不是注释 resource方法.

提示:要减少印刷错误,可以使用常量值:

Tip: To reduce typographical errors you could use constant values:

@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

看看 MediaType 类.

这篇关于泽西岛多种农产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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