RESTEasy:@FormParam、@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixPara

介绍:

In the first RESTEasy tutorial we have learnt the basics about REST Web services and we have tested a simple RESTful Web service. In this tutorial we will
show how to inject web application elements (form parameters, query parameters and more) into a RESTful Web service.

You can use the following annotations to bind HTTP requests to a RESTful web service:

@FormParam
@PathParam
@QueryParam
@HeaderParam
@CookieParam
@MatrixParam
 

Let's explore all the possible interactions.

@FormParam

The annotation @FormParam can be used to inject the parameters of a Web form into a RESTful Web service.
Here's an example:

resteasy tutorial

 

Here we are submitting a POST request containing two parameters email and password which
are translated into the parameters "e" and "p" of the login method.

 

Here's the full example:

<form method="POST" action="login">
    Email Address: <input type="text" name="email"><br>
    Password:      <input type="text" name="password">
    <input type="submit">
</form>

 

@Path("/")
public class LoginService
{
  @Path("login")
  @POST
  public String login(@FormParam("email") String e, @FormParam("password") String p) {   
     return "Logged with " + e + " " + p;
  }
}


As an alternative, you can bind the parameters email and password at class level, which can be useful if you need to re-use the same parameters across different
methods of the service.

public class User {
  @FormParam("email")
  private String email;

  @FormParam("password")
  private String password;
}


You would need to modify the REST method accordingly:

 

 @POST
 @Path("login")
 public String login(@Form User form) {
     return "Logged with " + form.email + " " + form.password;
 }

 

@PathParam

 

The @PathParam annotation binds the value of a path segment to a resource method parameter. For example, the following method would intercept an HTTP GET like http://server:port/login/12345 and
convert the PathParam "12345" into the String "id"

@Path("/")
public class LoginService
{
  @GET
  @Path("login/{zip}")
  public String login(@PathParam("zip") String id) {
     return "Id is " +id;
  }
}

As for @FormParam, you can embed the @PathParam declaration at class level, if you prefer.

 

@QueryParam

The @QueryParam annotation binds the value of a path segment to a resource method parameter. For example, the following method would intercept an HTTP GET like http://server:port/login?zip=12345 and
inject the query parameter "zip" into the method parameter "zip"

@Path("/")
public class LoginService
{
 @GET
 @Path("login/{zip}")
  public String login(@QueryParam("zip") String zip) {
     return "Id is " +id;
  }
}


QueryParam can be convenientely used with the DefaultValue annotation so that you can avoid a null pointer exception if no query parameter is passed.

 @GET
 @Path("login/{zip}")
 public String login(@DefaultValue("11111") @QueryParam("zip") String zip) {
     return "Id is " +id;
 }

 

As for @FormParam, you can embed the @PathParam declaration at class level, if you prefer.

@HeaderParam

The @HeaderParam annotation extracts information from the HTTP header and binds it to a method parameter. Example:

@GET
public String callService(@HeaderParam("User-Agent") String whichBrowser) {
  ...
}

 

@CookieParam

The @CookieParam annotation reads an information stored as a cookie and binds it to a method parameter. Example:

@GET
public String callService(@CookieParam("sessionid") String sessionid) {
  ...
}

 

@MatrixParam

The @MatrixParam annotation can be used to bind an expression containing several property=value to a method parameter. For example, supposing you were to invoke an URL like http://server:port/login;name=francesco;surname=marchioni

@GET
public String callService(@MatrixParam("name") String name,
                                @MatrixParam("surname") String surname) {
...
}

 

 

另外:

http://www.soapui.org/REST-Testing/understanding-rest-parameters.html

http://docs.oracle.com/cd/E19226-01/820-7627/6nisfjmk8/index.html

 

原文地址:http://www.xuebuyuan.com/2129382.html

猜你喜欢

转载自wb284551926.iteye.com/blog/2357388