RESTEasy:@FormParam、@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixParam说明

RESTEasy:@FormParam、@PathParam、@QueryParam、@HeaderParam、@CookieParam、@MatrixParam说明


@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:

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;"><form method="POST" action="login">
    Email Address: <input type="text" name="email"><br>
    Password:      <input type="text" name="password">
    <input type="submit">
</form>
</span>


<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; font-family: Arial, Helvetica, sans-serif; background: transparent;">@Path("/")</span>
<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">public class LoginService
{
  @Path("login")
  @POST
  public String login(@FormParam("email") String e, @FormParam("password") String p) {   
     return "Logged with " + e + " " + p;
  }
}
</span>


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.

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">public class User {
  @FormParam("email")
  private String email;</span>
<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">
  @FormParam("password")
  private String password;
}
</span>


You would need to modify the REST method accordingly:

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;"> @POST
 @Path("login")
 public String login(@Form User form) {
     return "Logged with " + form.email + " " + form.password;
 }
</span>


@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"

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">@Path("/")
public class LoginService
{
  @GET
  @Path("login/{zip}")
  public String login(@PathParam("zip") String id) {
     return "Id is " +id;
  }
}</span>

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"

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">@Path("/")
public class LoginService
{
 @GET
 @Path("login/{zip}")
  public String login(@QueryParam("zip") String zip) {
     return "Id is " +id;
  }
}
</span>


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

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;"> @GET
 @Path("login/{zip}")
 public String login(@DefaultValue("11111") @QueryParam("zip") String zip) {
     return "Id is " +id;
 }
</span>


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:

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">@GET
public String callService(@HeaderParam("User-Agent") String whichBrowser) {
  ...
}
</span>


@CookieParam

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

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">@GET
public String callService(@CookieParam("sessionid") String sessionid) {
  ...
}
</span>


@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

<span style="margin: 0px; padding: 0px; border: 0px; font-size: 14px; background: transparent;">@GET
public String callService(@MatrixParam("name") String name,
                                @MatrixParam("surname") String surname) {
...
}</span>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值