博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAX-RS 接收参数注意默认为非编码
阅读量:6121 次
发布时间:2019-06-21

本文共 2832 字,大约阅读时间需要 9 分钟。

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",     * not "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("/")@Encodedpublic 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

你可能感兴趣的文章
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
P127、面试题20:顺时针打印矩阵
查看>>
JS图片跟着鼠标跑效果
查看>>
[SCOI2005][BZOJ 1084]最大子矩阵
查看>>
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
【FJOI2015】金币换位问题
查看>>
数学之美系列二十 -- 自然语言处理的教父 马库斯
查看>>
Android实现自定义位置无标题Dialog
查看>>
面试总结
查看>>
Chrome浏览器播放HTML5音频没声音的解决方案
查看>>
easyui datagrid 行编辑功能
查看>>
类,对象与实例变量
查看>>
HDU 2818 (矢量并查集)
查看>>
【转】php字符串加密解密
查看>>
22. linux 常用命令
查看>>
ASP.Net 使用GridView模板删除一行的用法
查看>>
(十六)字段表集合
查看>>