Java – Jersey client / jax-rs and optional (not default) @ queryparam (client)
I have a restful API. Its document says that a query parameter is optional and does not provide default parameters Therefore, I can provide this value or not send it as a parameter in the get request
Example:
>Querya is required > queryb is optional (you can send get without it)
This should work:
http://www.example.com/service/endpoint?queryA=foo&queryB=bar
This should also be effective:
http://www.example.com/service/endpoint?queryA=foo
How do I do this for Jersey proxy's client interface? I don't have server-side code to connect, so I use org. Org through Jersey proxy glassfish. jersey. client. proxy. Webresourcefactory to generate client and server APIs to interact
Sample interface:
import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first,@QueryParam("queryB") String second); }
I know I can do another way:
@Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first);
But what happens when you have multiple optional fields? I don't want every possible mutation of them!
Solution
The interface is always
I can't believe it's easy:
import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first,@QueryParam("queryB") String second); }
Note what is different from the problem interface? no That's because that's the answer!
Do not use @ DefaultValue
If you want to default a parameter to a specific value, use the @ DefaultValue annotation in the parameter:
import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first,@QueryParam("queryB") @DefaultValue("default") String second); }
Pass null to the @ queryparam you don't want
If you want to make @ queryparam optional, the @ DefaultValue annotation is not applied To pass a value using the query parameter, just pass the value normally If you want the query parameters not to be displayed, just pass null!
import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @Path("/service") @Produces("application/json") public interface ServiceInterface { @Path("/endpoint") @GET public Response getEndpoint( @QueryParam("queryA") String first,// Pass null to this parameter to not put it in the GET request @QueryParam("queryB") String second); }
So call serviceinterface getEndpoint(“firstQueryParam”,“secondQueryParam”); requirement:
http://targethost.com/service/endpoint?queryA=firstQueryParam&queryB=secondQueryParam
And call serviceinterface getEndpoint(“firstQueryParam”,null); requirement:
http://targethost.com/service/endpoint?queryA=firstQueryParam
And Walla! No second query parameter!
The above is all the contents of Java – Jersey client / jax-rs and optional (not default) @ queryparam (client) collected by programming home for you. I hope this article can help you solve the program development problems encountered by Java – Jersey client / jax-rs and optional (not default) @ queryparam (client).
If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.