DrG*_*arl 7 java url spring spring-mvc
我在 Spring 中有一个 REST 组件。简化后,它看起来像这样:
@RequestMapping(value = "/route", method = RequestMethod.GET)
public Object thisIsTheMethod(@RequestParam(value = "value", required = false) List<String> values) {
return OtherClass.doTheThing(values);
}
Run Code Online (Sandbox Code Playgroud)
这用于使用任意长度的字符串列表。您可以通过多种方式做到这一点:
localhost:8080/route?value=this&value=that
Run Code Online (Sandbox Code Playgroud)
或者
localhost:8080/route?value=this,that
Run Code Online (Sandbox Code Playgroud)
现在,让我们说,我想在其中包含一个逗号一个字符串传递:value,1。我该怎么做?用 %2C 替换逗号会产生一个包含 2 个值的列表(“value”、“1”)。将任何内容放在引号中或转义引号中都有类似的问题。当我有多个参数并使用多值模式时,它看起来有效,但当我使用逗号分隔模式时则无效。
小智 3
您可以设置您的Delimiter. 逗号只是 org.springframework.boot.convert.DelimitedStringToCollectionConverter未设置时使用的默认逗号。如果您愿意,您可以Delimiter完全禁用它。对于您的代码,它看起来像这样:
import org.springframework.boot.convert.Delimiter;
@RequestMapping(value = "/route", method = RequestMethod.GET)
public Object thisIsTheMethod(@Delimiter(Delimiter.None) @RequestParam(value = "value", required = false) List<String> values) {
return OtherClass.doTheThing(values);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3369 次 |
| 最近记录: |