JAX-RS @ formparam and @ headerparam

Source: Internet
Author: User
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
  1. HTML>
  2. <Body>
  3. <H1> JAX-RS @ formquery testing
  4. <Form action = "rest/user/Add" method = "Post">
  5. Name: <input type = "text" name = "name"/>
  6. Age: <input type = "text" name = "Age"/>
  7. <Input type = "Submit" value = "Add User"/>
  8. </Form>
  9. </Body>
  10. </Html>
html><body>

Processing:

Java code
  1. @ Path ("/user ")
  2. Public class userservice {
  3. @ Post
  4. @ Path ("/Add ")
  5. Public Response adduser (
  6. @ Formparam ("name") string name,
  7. @ Formparam ("Age") int age ){
  8. Return response. Status (200)
  9. . Entity ("adduser is called, name:" + name + ", age:" + age)
  10. . Build ();
  11. }
@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
  1. @ Path ("/users ")
  2. Public class userservice {
  3. @ Get
  4. @ Path ("/get ")
  5. Public Response adduser (@ headerparam ("User-Agent") string useragent ){
  6. Return response. Status (200)
  7. . Entity ("adduser is called, useragent:" + useragent)
  8. . Build ();
  9. }
@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

In addition, the Java code

  1. @ Path ("/users ")
  2. Public class userservice {
  3. @ Get
  4. @ Path ("/get ")
  5. Public Response adduser (@ context httpheaders headers ){
  6. String useragent = headers. getrequestheader ("User-Agent"). Get (0 );
  7. Return response. Status (200)
  8. . Entity ("adduser is called, useragent:" + useragent)
  9. . Build ();
  10. }
@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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.