Java - Getting the URL's parameter

167 views Asked by At

I have a URL something like this

I'm using play framework

HTTP Routes:

GET     /students                    controllers.Application.Get()
POST    /students                    controllers.Application.Post()
PUT     /students/{studentNo}        controllers.Application.Put()

How will I get the {studentNo} value? For example I'm using a postman(rest client) and I typed something in the url http://localhost:9000/students/2012111222 , how will I get the "2012111222". Do I need to split the URL's value just to get that parameter or is there something else. Thank you.!

3

There are 3 answers

3
Christina On BEST ANSWER

As you can see in Play's routing documentation you can use the colon syntax to define that some part of your route URL is a variable and pass that variable to the controller method, ie:

POST  /students/:studentNo        controllers.Application.Post(studentNo: Long)
2
Sivailango On

You can't use the /students/:studentNo, should use /students?number=111 in POST request and get the value using DyanamicForm or request().getQueryString("number") without mention the controllers.Application.Post(studentNo: Long)

1
skrtbhtngr On

Do something like this:

If you have a string like:

String s="POST    /students/12        controllers.Application.Post()";

You can retrieve "12" like:

int studentNo = Integer.parseInt(s.split("\\s+|,\\s*|\\.\\s*")[1].split("/")[2]);