我们有许多 @RestController 接收用户编写的通用语言短语。短语可以很长并且包含标点符号,例如句号,当然还有逗号。
简化控制器示例:
@RequestMapping(value = "/countphrases", method = RequestMethod.PUT)
public String countPhrases(
@RequestParam(value = "phrase", required = false) String[] phrase) {
return "" + phrase.length;
}
Spring Boot 默认行为是以逗号分割参数值,因此之前的控制器使用以下 url 进行调用:
[...]/countphrases?phrase=john%20and%20me,%20you%and%her
将返回“2”而不是我们想要的“1”。事实上,使用逗号分割,前面的调用相当于:
[...]/countphrases?phrase=john%20and%20me&phrase=you%and%her
我们使用自然语言,我们需要准确分析用户如何编写短语并准确了解他们写了多少个短语。
我们尝试了这个解决方案:https://stackoverflow.com/a/42134833/1085716 https://stackoverflow.com/a/42134833/1085716适应我们的 Spring Boot 版本(2.0.5)后:
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
// we hoped this code could remove the "split strings at comma"
registry.removeConvertible(String.class, Collection.class);
}
}
但这不起作用。
有人知道如何在 Spring Boot 2.0.5 中全局删除“以逗号分隔字符串参数”行为吗?