How to use query parameters in a Java jersey application?

I am following a tutorial, and also used the question https://stackoverflow.com/a/166269/2129 . Here is my Java class:

package com.crunchify.tutorial; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; import org.json.simple.JSONObject; @Path("api") public class CrunchifyAPI { @SuppressWarnings("unchecked") @GET @Path("/get") @Consumes(MediaType.TEXT_PLAIN) public String get( @DefaultValue("111") @QueryParam("user") int user, @Context UriInfo uriInfo ) { MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters(); String nameParam = queryParams.getFirst("user"); System.out.println("Data Received: " + uriInfo.getRequestUri().getQuery() + " | " + nameParam); JSONObject obj = new JSONObject(); obj.put("auth", true); String ret = JSONObject.toJSONString(obj); return ret; } } 

Here is what I get from the postman:

 GET>> localhost/api/get?user=123 

Answer:

 {"auth":true} 

Server Console:

 Starting Crunchify Embedded Jersey HTTPServer... Started Crunchify Embedded Jersey HTTPServer Successfully !!! Data Received: ?user=123 | null User Authenticated: true 

I tried with passing String, Integer, etc., but nothing works. The uri information is printed correctly, and the answer back is also beautiful. The problem is that I am not getting a read parameter in Java Code. I will need to pass many other parameters as soon as I can do this. Please suggest. Thanks!!

+5
source share
3 answers

I think you are trying too hard. As far as I can tell, doing the following should get what you want if you call localhost / api / get? User = 123:

 package com.crunchify.tutorial; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Consumes; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; import org.json.simple.JSONObject; @Path("api") public class CrunchifyAPI { @SuppressWarnings("unchecked") @GET @Path("/get") @Consumes(MediaType.TEXT_PLAIN) public String get( @DefaultValue("111") @QueryParam("user") Integer user, @Context UriInfo uriInfo ) { System.out.println("Data Received: " + uriInfo.getRequestUri().getQuery() + " | " + name); JSONObject obj = new JSONObject(); obj.put("auth", true); String ret = JSONObject.toJSONString(obj); return ret; } } 

All unnecessary materials with the query string are not needed if you only need the information passed in the user parameter.

+2
source

@QueryParam (user) int user

the value of this user int should be 123

See https://www.mkyong.com/webservices/jax-rs/jax-rs-queryparam-example/

+1
source

Well, I think you have a problem with Java types.

If your user is an Integer , you must first pass it a String if you want to work with String ( Integer.toString() or String.valueof() ).

But the way you pass the parameter bothers me, I'm not sure if you can pass integers by types of simple copper types.

0
source

Source: https://habr.com/ru/post/1262253/


All Articles