Continue Learning jboss reseteasy parameters today. What we want to learn today is @ PathParam
And @ matrixParam
1) @ pathparam
First look at the example:
@ Path ("/users ")
Public class UserRestService {
@ GET
@ Path ("{id }")
Public Response getUserById (@ PathParam ("id") String id ){
Return Response. status (200). entity ("getUserById is called, id:" + id). build ();
}
}
The URL is as follows:
/Users/22667788"
Match (@ PathParam ("id") String id)
Therefore, output:
GetUserById is called, id: 22667788
2) @ pathparam matches multiple output parameters:
@ Path ("/users ")
Public class UserRestService {
@ GET
@ Path ("{year}/{month}/{day }")
Public Response getUserHistory (
@ PathParam ("year") int year,
@ PathParam ("month") int month,
@ PathParam ("day") int day ){
String date = year + "/" + month + "/" + day;
Return Response. status (200)
. Entity ("getUserHistory is called, year/month/day:" + date)
. Build ();
}
}
URl: "/users/2011/06/30"
Matching:
GetUserHistory is called, year/month/day: 2011/6/30
3) matrixparam
This is mainly for splitting parameters, such:
@ GET
@ Path ("{year }")
Public Response getBooks (@ PathParam ("year") String year,
@ MatrixParam ("author") String author,
@ MatrixParam ("country") String country ){
Return Response
. Status (200)
. Entity ("getBooks is called, year:" + year
+ ", Author:" + author + ", country:" + country)
. Build ();
}
URL:
"/Books/2011 /"
Result:
GetBooks is called, year: 2011, author: null, country: null
URL:/books/2011; author = mkyong; country = malaysia
Result:
GetBooks is called, year: 2011, author: mkyong, country: malaysia