Seckin Tozlu
Seckin Tozlu

Reputation: 556

CXF Rest Client - Pass HTTP Query Parameter by Default

I am writing a Proxy based Rest client using Apache CXF and I would like to pass some of the query parameters without having to pass them in my "search" method in the proxy interface. I tried using @DefaultValue but with that you still have to define a method parameter which I have to pass the same exact value everywhere. Is there a way to tell CXF to pass a query parameter with the same value all the time? That way I can remove some of the unnecessary parameters from proxy methods.

    @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);

Upvotes: 1

Views: 2509

Answers (1)

Avi
Avi

Reputation: 21866

Why won't you try a different approach. create an object SearchParameters that will be just a plain 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
}

Now change the search definition to look like this:

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

In the search implementation just call the static builder from SearchParameters with request and there you have it.

Upvotes: 1

Related Questions