如何在cxf中的消息body中设置自定义object?

How to set custom object in body of message in cxf?

我有一个带有自定义方法 (GET) 的 REST 网络服务。

@GET
@Path("/custom")
public UIResponse custom(final UIParameters uiParameters){...}

如您所见,此方法有一个参数。这是我的习惯 object.

Object UIParameters 是根据作为查询字符串给出的参数构建的。

例如。 http://example.com/custom?objectType=article

UIParameters object 将包含一个字段:

UIParameters {
   String objectType = "article";
}

我尝试使用 InInterceptor 从 URL 获取此参数,构建 UIParameter object 并设置消息内容。不幸的是,它不起作用。 之后,我为 UIParameters 提供了 MessageBodyReader,但它仍然不起作用。

我应该怎么做才能实现这个目标?

谢谢

更新:

在 InInterceptor 中,我已将查询字符串复制到 http headers。现在 URL 的一部分,用户放置参数可以在我的 MessageBodyReader 中访问。在这里我可以构建我的 object UIParameters。

一切正常,但我认为这个解决方案不是最好的。

有人知道更好的解决方案吗?

AnnotationQueryParam("") 允许获取所有注入的查询参数。

您不需要拦截器,这不是推荐的方式。请参阅 CXF 文档 http://cxf.apache.org/docs/jax-rs-basics.html#JAX-RSBasics-Parameterbeans

@GET
@Path("/custom")
public UIResponse custom(@QueryParam("") UIParameters uiParameters)

class UIParameters {
      String objectType;
}

如果您想使用查询参数自己构建 bean,请使用 @Context UriInfo 注释

@GET
@Path("/custom")
public UIResponse custom( @Context UriInfo uriInfo){
     MultivaluedMap<String, String> params = uriInfo.getQueryParameters();
     new UIParameters().Builder()
        .objectType(params.getFirst("type"))
        .build();
}