RestEasy传值方式

一、@pathparam

   @PathParam 是一个参数注解,可以将一个 URL 上的参数映射到方法的参数上,它可以映射到方法参数的类型有基本类型、字符串、或者任何有一个字符串作为构造方法参数的 Java 对象、或者一个有字符串作为参数的静态方法 valueOf 的 Java 对象(一般是从Get的url中获取参数值)。

@GET  
@Path("delProByPNumber/{param}")  
@Produces("application/json; charset=utf-8")  
public Response delPro(<strong>@PathParam("param") String  pNumber</strong>){  
   session.delete("production.delete", pNumber);  
   session.commit();  
   List list=queryAllPro();  
   return Response.status(201).entity(list).build();  
}

 访问路径:http://localhost:8888/Invoice/optPro/delProByPNumber/7

 @pathparem中,URL中只出现参数的值,不出现键值对,比如: “/users/2011/06/30” 

@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();  
 }

另外:{var}代表只能有一个路径名,如:a;而{var:.*}代表可以是任意多个路径名,如:a/b/c

/**
 * {var:.*}代表可以是任意多个路径名,如:a/b/c
 */
@Path("/pathRegular1/{var:.*}/{msg}")
@GET
public String pathRegular1(@PathParam("msg") String m){
   return m;
}
	
/**
 * {var}代表只能有一个路径名,如:a
*/
@Path("/pathRegular1/{var}/{msg}")
@GET
public String pathRegular2(@PathParam("msg") String m){
   return m;
}

 二、@queryParam

   @queryParam从/XXX/路径/?num=5中获取参数

@GET  
@Path("addAcc/parameters")  
@Produces("application/json; charset=utf-8")  
public Response addCus(  
     @QueryParam("accname") String accname,  
     @QueryParam("balance") long balance,  
     @QueryParam("department") String   department,   
    ) {  
     Account account=new Account();  
     account.setAccname(accname);  
     account.setBalance(balance);  
  return Response.status(201).entity(queryOne(accname)).build();
}  

访问路径:http://localhost:8888/Invoice/optAcc/addAcc/parameters?accname=rr&balance=99999&department=hedaparameters后面为传入参数

Path("/users")  
public class UserService {     
  @GET  
  @Path("/query")  
  public Response getUsers(  
  @QueryParam("from") int from,  
  @QueryParam("to") int to,  
  @QueryParam("orderBy") List<String> orderBy) {  
    return Response.status(200).entity("getUsers is called, from : " + from + ", to : " + to  + ", orderBy" + orderBy.toString()).build();  
} 

URL输入为:users/query?from=100&to=200&orderBy=age&orderBy=name 此时,输出为: getUsers is called, from : 100, to : 200, orderBy[age, name]。

注意:跟@pathparam不同,@queryparam中,指定的是URL中的参数是以键值对的形式出现的,而在程序中 @QueryParam("from") int from则读出URL中from的值

 三、@FormParam

  将表单中的字段映射到方法调用上,对于此类方式一般提交方式为Post。

  例如,对于下面的表单:

<form method="POST" action="/resources/service">
First name: 
<input type="text" name="firstname">
<br>
Last name: 
<input type="text" name="lastname">
</form>

通过 post 方法提交,处理该请求的方法为:

Path("/")
public class NameRegistry {
    @Path("/resources/service")
    @POST
    public void addName(@FormParam("firstname") String first, @FormParam("lastname") String last) {...}
}    

 访问路径:http://localhost:8888/resources/services/hello/getName

也可以添加application/x-www-form-urlencoded来反序列化 URL 中的多参数:

@Path("/")
public class NameRegistry {
   @Path("/resources/service")
   @POST
   @Consumes("application/x-www-form-urlencoded")
   public void addName(@FormParam("firstname") String first, MultivaluedMap<String, String> form) {...}
}       

 参见:http://blog.csdn.net/programmeryu/article/details/50536024

           http://ju.outofmemory.cn/entry/144017

           http://www.cnphp6.com/archives/29400

原文地址:https://www.cnblogs.com/moonandstar08/p/5763184.html