質問

I created a java rest resource with 2 methods both listening on the same path, but one with @QueryParam annotation as method parameter

@Path("test")
public class TestResource {

    @GET
    public Response filter(){  
        System.out.println("init");
        return Response.accepted("Hello world").build();
    }

    @GET    
    public Response filterQuery(@QueryParam("filter") String filterQuery) {
        return Response.accepted("filter: " + filterQuery).build();
        //... parsing filter
    } 
}

But when I try GET on ../test?filter=name:Juraj;location:Slovakia or on ../test it is always calling the filter() method

Is it not enough to define the QueryParam to get the second method be called? I would like to offer the user to parametrize on the same url, not on the /test/filter?name=...

Can I do this somehow? Using apache-cxf 3.0.0-milestone2

役に立ちましたか?

解決

Defining a @QueryParam does not mean that this parameter is required. If the query-string is empty or not including the defined query-param the value of the parameter will be null.

So both methods are ambiguous and seems like the first one wins. If you want two methods you can easily check for null and call the other method.

@GET    
public Response filterQuery(@QueryParam("filter") String filterQuery) {
    if (filterQuery == null) {
        return filter();
    }
    return Response.accepted("filter: " + filterQuery).build();
} 

private Response filter() {  
    return Response.accepted("Hello world").build();
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top