How to get query string parameters in java play framework?
Asked Answered
A

5

25

I'm very new to java play framework. I've set up all the normal routes like /something/:somthingValue and all the others. Now I want to create route the accepts query parameters like

/something?x=10&y=20&z=30

Here I want to get all the params after "?" as key==>value pair.

Allegorize answered 9/4, 2013 at 16:59 Comment(0)
U
38

You can wire in your query parameters into the routes file:

http://www.playframework.com/documentation/2.0.4/JavaRouting in section "Parameters with default values"

Or you can ask for them in your Action:

public class Application extends Controller {

    public static Result index() {
        final Set<Map.Entry<String,String[]>> entries = request().queryString().entrySet();
        for (Map.Entry<String,String[]> entry : entries) {
            final String key = entry.getKey();
            final String value = Arrays.toString(entry.getValue());
            Logger.debug(key + " " + value);
        }
        Logger.debug(request().getQueryString("a"));
        Logger.debug(request().getQueryString("b"));
        Logger.debug(request().getQueryString("c"));
        return ok(index.render("Your new application is ready."));
    }
}

For example the http://localhost:9000/?a=1&b=2&c=3&c=4 prints on the console:

[debug] application - a [1]
[debug] application - b [2]
[debug] application - c [3, 4]
[debug] application - 1
[debug] application - 2
[debug] application - 3

Note that c is two times in the url.

Utham answered 9/4, 2013 at 18:4 Comment(6)
I'm not able to see getQueryString() method of request(). is it because I'm using play 2.0 and you're using 2.0.4 ?Allegorize
You are right, playframework.com/documentation/api/2.0/java/play/mvc/… does not contain getQueryString() but request().queryString() can give you all you need.Utham
Thanks a lot man. can you suggest me some good tutorials on it? Except it's documentation.Allegorize
Great, then please mark my answer accepted. A list of Play tutorials can be found on groups.google.com/forum/?fromgroups=#!topic/play-framework/…Utham
One more issue, as play 2.0 has not getQueryString() method and Arrays.toString(entry.getValue()) returns value with [] what if I don't want those [] ? I just want value as we're getting with getQueryString() method. Do I've to switch to latest version of Play ?Allegorize
I solved it, and it was silly mistake. I just did entry.getValue()[0]Allegorize
R
22

In Play 2.5.x, it is made directly in conf/routes, where one can put default values:

# Pagination links, like /clients?page=3
GET   /clients              controllers.Clients.list(page: Int ?= 1)

In your case (when using strings)

GET   /something            controllers.Somethings.show(x ?= "0", y ?= "0", z ?= "0")

When using strong typing:

GET   /something            controllers.Somethings.show(x: Int ?= 0, y: Int ?= 0, z: Int ?= 0)

See: https://www.playframework.com/documentation/2.5.x/JavaRouting#Parameters-with-default-values for a longer explanation.

Rawson answered 7/8, 2016 at 23:0 Comment(0)
D
10

You can get all query string parameters as a Map:

Controller.request().queryString()

This method return a Map<String, String[]> object.

Damnable answered 1/11, 2014 at 12:10 Comment(0)
C
0

In Java/Play 1.x you get them with:

    Request request = Request.current();
    String arg1 = request.params.get("arg1");

    if (arg1 != null) {
        System.out.println("-----> arg1: " + arg1);
    } 
Cervine answered 26/2, 2016 at 15:29 Comment(0)
S
0

You can use FormFactory:

DynamicForm requestData = formFactory.form().bindFromRequest();
String firstname = requestData.get("firstname");
Satterlee answered 5/1, 2018 at 21:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.