将URI路径传递给JAX-RS提供者 [英] Passing the URI Path to the JAX-RS Providers

查看:92
本文介绍了将URI路径传递给JAX-RS提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近实施了Jersey JAX-RS Rest服务.我创建了一个JIBX提供程序,该提供程序允许在XML和Java类型之间进行编组和编组.我也想通过在URL路径中指定版本来对我的服务进行版本控制.版本控制将包括用于封送和解组Java类型的消息绑定的版本.

I recently implemented a Jersey JAX-RS Rest service. I created a JIBX provider that allows one to unmarshal and marshall between XML and Java types. I would like to also version my service by specifying the version in the URL path. Versioning would include the version of message binding used to marshal and unmarshall the Java types.

因此,必须将版本传递给定制JIBX提供程序,因此必须将其传递给包含该版本的URL路径.但是,提供者接口(MessageBodyWriterMessageBodyReader)在其接口方法中未提供URI路径.

Therefore, it is necessary that the version is passed to the custom JIBX provider, and therefore the URL path that contains the version. However, the Provider interfaces (MessageBodyWriter and MessageBodyReader) do not provide the URI path in their interface methods.

以下是MessageBodyWriter接口的writeTo()方法的方法签名:

The following is the method signature of the writeTo() method of the MessageBodyWriter interface:

writeTo(Object, Type, Annotation[], MediaType, MultivaluedMap, OutputStream)

此方法参数不包含路径uri,因此,定制的jibx提供程序无法知道应使用哪种消息绑定版本来编组Java类型.有没有解决的办法?

This method parameters does not contain the path uri, therefore, the custom jibx provider cannot know which message binding version it should use to marshall the Java type. Is there a way around this?

推荐答案

如果您想要的是比HttpServletRequest更特定于JAX-RS的内容,则可以注入javax.ws.rs.core.UriInfo.

If you want something a bit more JAX-RS specific than HttpServletRequest, you can inject a javax.ws.rs.core.UriInfo.

public class MyProvider implements MessageBodyWriter {
    @javax.ws.rs.core.Context
    javax.ws.rs.core.UriInfo uriInfo;
}

我假设您正在使用@ javax.ws.rs.PathParam来捕获path参数.然后,您可以潜在地使用UriInfo.getPathParameters().您还可以回退至UriInfo.getPathSegments()以获得所需的信息.这省去了您自己解析请求URI的麻烦.任何JAX-RS实现都应该能够做到这一点.

I'm assuming that you're using a @javax.ws.rs.PathParam to capture the path parameter. You can then potentially use UriInfo.getPathParameters(). You can also fall back to UriInfo.getPathSegments() to get the information you're looking for. This saves you the trouble of parsing the request URI yourself. Any JAX-RS implementation should be able to do this.

这篇关于将URI路径传递给JAX-RS提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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