【问题标题】:CXF Rest Client - Pass HTTP Query Parameter by DefaultCXF Rest Client - 默认传递 HTTP 查询参数
【发布时间】:2013-06-21 11:19:38
【问题描述】:

我正在使用 Apache CXF 编写基于代理的 Rest 客户端,我想传递一些查询参数,而不必在代理接口的“搜索”方法中传递它们。我尝试使用 @DefaultValue 但是你仍然必须定义一个方法参数,我必须在任何地方传递相同的确切值。有没有办法告诉 CXF 始终传递具有相同值的查询参数?这样我就可以从代理方法中删除一些不必要的参数。

    @GET
    @Path("/{version}/{accountId}/search")
    @Produces(MediaType.APPLICATION_JSON)
    public String search(@PathParam("version") String version,
                         @PathParam("accountId") String accountId,
                         @DefaultValue("")@QueryParam("q") String queryString,
                         @DefaultValue("")@QueryParam("category") String category,
                         @DefaultValue("1")@QueryParam("page") int page,
                         @DefaultValue("50")@QueryParam("limit") int limit,
                         @DefaultValue("all")@QueryParam("response_detail") String responseDetail);

【问题讨论】:

    标签: java rest client cxf


    【解决方案1】:

    您为什么不尝试不同的方法。创建一个对象SearchParameters,它只是一个普通的pojo:

    public class SearchParameters {
         private String version;
         private String accountId;
         // Other fields
    
         public static SearchParameters(HttpServletRequest request) {
            // Here you use the getParameterMap of the `request` object to get
            // the query parameters. Look here: http://stackoverflow.com/questions/6847192/httpservletrequest-get-query-string-parameters-no-form-data
    
            // Everything that was not passed in the parameters
            // just init with default value as you wish.
         }
    
         // Getters and setters here
    }
    

    现在将search 定义更改为如下所示:

    @GET
    @Path("/{version}/{accountId}/search")
    @Produces(MediaType.APPLICATION_JSON)
    public String search(@PathParam("version") String version,
                         @PathParam("accountId") String accountId,
                         @Context HttpServletRequest request);
    

    search 实现中,只需使用requestSearchParameters 调用静态构建器,就可以了。

    【讨论】:

    • 抱歉,我还没有机会尝试。优先级改变了:) 当我回到这个问题时我会告诉你的。谢谢你..
    猜你喜欢
    • 2019-10-17
    • 2016-01-02
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多