JAX-RS 接收参数注意默认为非编码

API文档部分
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FormParam {

    /**
     * Defines the name of the form parameter whose value will be used
     * to initialize the value of the annotated method argument. The name is
     * specified in decoded form, any percent encoded literals within the value
     * will not be decoded and will instead be treated as literal text. E.g. if
     * the parameter name is "a b" then the value of the annotation is "a b",
     * <i>not</i> "a+b" or "a%20b".
     */
    String value();
}

  说的是默认不进行编码。

以下介绍出自:

https://access.redhat.com/documentation/en-US/JBoss_Enterprise_Web_Platform/5/html/RESTEasy_Reference_Guide/_Encoded_and_encoding.html

 
JAX-RS allows you to get encoded or decoded  @*Params and specify path definitions and parameter names using encoded or decoded strings.
The  @javax.ws.rs.Encoded annotation can be used on a class, method, or parameter. By default, injected  @PathParam and  @QueryParam are decoded. Adding the  @Encoded annotation means that the value of these parameters will be provided in encoded form.
@Path("/")
public class MyResource {

  @Path("/{param}")
  @GET
  public String get(@PathParam("param") @Encoded String param) {...}

  

In the previous example, the value of the  @PathParam injected into the  param of the  get() method will be URL encoded. Adding the  @Encoded annotation as a parameter annotation triggers this effect.
You can also use the  @Encoded annotation on the entire method and any combination of  @QueryParam or  @PathParam's values will be encoded.
@Path("/")
public class MyResource {
  
   @Path("/{param}")
   @GET
   @Encoded
   public String get(@QueryParam("foo") String foo, @PathParam("param") String param) {}
}

  

In this example, the values of the  foo query parameter and the  param path parameter will be injected as encoded values.
You can also set the default to be encoded for the entire class.
@Path("/")
@Encoded
public class ClassEncoded {
  
   @GET
   public String get(@QueryParam("foo") String foo) {}
}

  

The  @Path annotation has an attribute called  encode. This controls whether the literal part of the value supplied (that is, the characters that are not part of a template variable) are URL-encoded. If true, any characters in the URI template that are not valid will be automatically encoded. If false, then all characters must be valid URI characters. By default, the encode attribute is set to true. (You can also encode the characters by hand.)
@Path(value="hello%20world", encode=false)

  

As with  @Path.encode(), this controls whether the specified query parameter name should be encoded by the container before it tries to find the query parameter in the request.
@QueryParam(value="hello%20world", encode=false)

  

转载于:https://www.cnblogs.com/let5see/p/4389597.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值