Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send query parameters to POST request from Java client, and receive using @QueryParam

I'm trying to write a basic web service that uses JAX-RS @QueryParam annotation. My Java client sends a POST request using HttpURLConnection. The call is being made to the web service, but the parameters are not getting sent correctly (They remain null). I know there are lot of questions related to this on StackOverflow, but I couldn't find a convincing solution. It'll be great if somebody points out the mistake in the code below. (I've simplified the code for posting here).

Here's the web service code:

@POST
@Path("/notify")
@Consumes("*/*")
public Response notifyUser(@QueryParam("notification") String notification, @QueryParam("applicationName") String applicationName) {
    System.out.println("Notification: " + notification);
    System.out.println("Application: " + applicationName);
    return Response.status(200).entity(notification + " from " + applicationName).build();
}

And this is Java client:

public static void main(String args[]) {
    URL url;
    HttpURLConnection conn = null;
    try {
         url = new URL("http://localhost:8080/...");
         conn = (HttpURLConnection) url.openConnection();
         conn.setDoOutput(true);
         conn.setRequestMethod("POST");
         conn.setDoInput(true);
         conn.setDoOutput(true);
         conn.setRequestProperty("Content-Type", "application/json");
         conn.setRequestProperty("charset", "UTF-8");
         OutputStream os = conn.getOutputStream();
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new NameValuePair("notification", "Notification string"));
         params.add(new NameValuePair("applicationName", "MyApp"));
         os.write(getQuery(params).getBytes("UTF-8"));
         os.flush();
         os.close();
         return conn.getResponseCode();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return -1;
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;

    for (NameValuePair pair : params) {
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
    }

    return result.toString();
}

My web service is getting called, but the parameter values are received as null. Please help.

like image 717
Akhil Dixit Avatar asked Sep 04 '25 03:09

Akhil Dixit


2 Answers

To bind the value(s) of a HTTP query parameter to a resource method parameter using @QueryParam, you need to send your parameters in the query string of the URL:

3.4. Query

The query component contains non-hierarchical data that, along with data in the path component, serves to identify a resource within the scope of the URI's scheme and naming authority (if any). The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.

[...]

By default, in GET requests, the parameters are sent as part of the URL. In POST requests, the parameters are sent in the body of the request.

To send query parameters in POST requests with HttpURLConnection, append them to the requested URL, as following:

List<NameValuePair> params = new ArrayList<>();
params.add(new NameValuePair("param1", "value1"));
params.add(new NameValuePair("param2", "value2"));

String query = getQuery(params);
URL url = new URL("http://localhost:8080/api" + "?" + query);
URLConnection connection = url.openConnection();

...
like image 74
cassiomolin Avatar answered Sep 05 '25 21:09

cassiomolin


@QueryParam only recognizes parameters which are part of the URL. You need to use @FormParam instead in order to read the parameters from the POST body.

like image 29
wero Avatar answered Sep 05 '25 21:09

wero