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)

  

时间: 2024-11-13 03:45:11

JAX-RS 接收参数注意默认为非编码的相关文章

[转] Request 接收参数乱码原理解析

起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" culture="zh-CN"/> 当前台提交

Request 接收参数乱码原理解析

起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" culture="zh-CN"/> 当前台提交

实用———springmvc接收参数校验

https://www.cnblogs.com/funyoung/p/8670550.html https://www.cnblogs.com/monkeydai/p/10068547.html [email protected] 两粒种子,一片森林. 首页 新随笔 联系 订阅 管理 随笔 - 21  文章 - 0  评论 - 8 两粒种子,一片森林. 首页 新随笔 联系 订阅 管理 随笔 - 21  文章 - 0  评论 - 8 SpringMVC参数校验 使用SpringMVC时配合hibe

Request 接收参数乱码原理解析一:服务器端解码原理

“Server.UrlDecode(Server.UrlEncode("北京")) == “北京””,先用UrlEncode编码然后用UrlDecode解码,这条语句永远为true吗?答案是否定的,结果可能与很多人预想的不大一样.本文主要分析这一问题出现的原理,研究下Server.UrlEncode(),Server.UrlDecode(),Request["xxx"]三个函数与编码方式的关系. 1. 问题出现的情景 网站采用了GB2312编码,在Web.confi

Struts2(四)Action一接收参数

一.属性接收参数并输出 导入struts2的包,导入需要的包 和struts.xml配置文件 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/str

Action属性接收参数

一.action的属性(地址栏传参)接收参数:如果使用的JDK属性不一致,则会使得传值无法实现. 解决办法: 1.系统自身需要用到的JDK(window——>属性——>Java——>Installed JREs——>选择符合条件的JDK版本) 2.设置JDK级别(项目右键——>属性——>Java Compiler——>Compiler compliance level——>选择符合条件的级别) 3.Tomcat下的JDK版本(Window——>属性——

Python 必选参数,默认参数,可变参数,关键字参数和命名关键字参数

Py的参数还真是多,用起来还是很方便的,这么多参数种类可见它在工程上的实用性还是非常广泛的. 挺有意思的,本文主要参照Liaoxuefeng的Python教程. #必选参数 def quadratic(a, b, c): if not isinstance(a, (int, float)) or not isinstance(b, (int, float)) or not isinstance(c, (int, float)): raise TypeError('bad operand type

springmvc传递参数与接收参数

springmvc和structs的接收参数的映射原理是一样的一.表单代码<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3

php与js中函数参数的默认值设置

php函数参数默认值设置: <?phpfunction test($val=3){   echo $val."<br/>";}test(11);test();?> javascript函数参数默认值设置function test(){alert("test函数");} function test2(val,func){ val=val||"";//利用js中的或操作,第一个参数值为false(为空)时返回第二个参数的值.第