Amresh Kadian
Amresh Kadian

Reputation: 191

How to use the query parameters in Java Jersey Application?

I am following a tutorial and also used the Stackoverflow question here. 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;
    }
}

Following is what I am GET'ing from postman:

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

Response is:

{"auth":true}

Server console:

Starting Crunchify's Embedded Jersey HTTPServer...

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

I have tried with passing String, Integer etc but nothing works. The uri Info is getting printed correctly and the response back is also fine. The issue is that I am not getting the parameter to be read in Java Code. I will need to pass many other parameters once I am able to get this going. Please suggest. Thanks!!

Upvotes: 12

Views: 26657

Answers (3)

Nielsvh
Nielsvh

Reputation: 1219

I think you're trying too hard. As far as I can tell, doing the following should get you 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 that extra stuff with the query string isn't needed if all you need is the information passed in the user parameter.

Upvotes: 6

user8559497
user8559497

Reputation:

Well, I think you're having a problem with Java Types.

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

But the way you're passing the parameter is bothering me, I'm not sure if you can pass integers by text plain medi types.

Upvotes: 0

Kalpesh Soni
Kalpesh Soni

Reputation: 7257

@QueryParam("user") int user

the value of that user int should be 123

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

Upvotes: 1

Related Questions