5

Passing Query Parameters to a REST Endpoint in Jersey

 3 years ago
source link: https://nixmash.com/java/passing-query-parameters-to-a-rest-endpoint-in-jersey/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Passing Query Parameters to a REST Endpoint in Jersey

Keywords: Java, Jersey, JAX-RS, Query Parameters, REST, Microservices, %3F, Question Mark

In this post I'll show you first how NOT to pass query parameters to a REST Endpoint in Jersey, then the correct way.

How NOT To Pass Query Parameters

We're going to pass a Solr search string with a query parameter "q" in the usual format of http://end/point?q=query. We'll start with our url path constant.

qparams0304a.png

And then populate it with MessageFormat and pass it to our Jersey Request Builder.

qparams0304b.png

The sorry result from our Posts Microservice is a 404. (Keyword: %3F)

qparams0304c.png

The Correct Way to Pass Query Parameters to a REST Endpoint in Jersey

We'll repeat the process, this time the correct way. First with an updated path constant containing no query parameter info.

qparams0304d.png

We pass the base path directly to our Jersey Client Builder.

qparams0304e.png

…where we add the Query Parameter in our Jersey Request.

qparams0304f.png

With a much happier result.

qparams0304g.png

CategoriesJavaTagsJava, Jersey, Microservices, REST, Solr


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK