Continue learning about @ formparam and @ headerparam in the JAX-RS today
1 @ formparam
The function is to bind the front-end HTML. Let's look at the example first.
Java code
Html>
<Body>
<H1> JAX-RS @ FormQuery Testing
<Form action = "rest/user/add" method = "post">
Name: <input type = "text" name = "name"/>
Age: <input type = "text" name = "age"/>
<Input type = "submit" value = "Add User"/>
</Form>
</Body>
</Html>
Processing:
Java code
@ Path ("/user ")
Public class UserService {
@ POST
@ Path ("/add ")
Public Response addUser (
@ FormParam ("name") String name,
@ FormParam ("age") int age ){
Return Response. status (200)
. Entity ("addUser is called, name:" + name + ", age:" + age)
. Build ();
}
For front-end HTML browsing, such:
Http: // localhost: 8080/RESTfulExample/UserForm.html
A common HTML form will appear. When the button is submitted, it will go
Http: /localhost: 8080/RESTfulExample/rest/user/add
In this way, REST will match the addUser method, so the data submitted in the form will be output.
2 In the JAX-RS, there are two ways to get the http request Header,
1) @ headparam
Java code
@ Path ("/users ")
Public class UserService {
@ GET
@ Path ("/get ")
Public Response addUser (@ HeaderParam ("user-agent") String userAgent ){
Return Response. status (200)
. Entity ("addUser is called, userAgent:" + userAgent)
. Build ();
}
When the access URL:
/Users/get
Will output the HTTP header information, namely:
AddUser is called, userAgent: Mozilla/5.0 (Windows NT 6.1; rv: 5.0) Gecko/20100101 Firefox/5.0
The other method is,
Java code
@ Path ("/users ")
Public class UserService {
@ GET
@ Path ("/get ")
Public Response addUser (@ Context HttpHeaders headers ){
String userAgent = headers. getRequestHeader ("user-agent"). get (0 );
Return Response. status (200)
. Entity ("addUser is called, userAgent:" + userAgent)
. Build ();
}
You can achieve the same purpose.