【发布时间】:2019-09-11 10:09:44
【问题描述】:
我们有许多@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 在适应我们的 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中全局删除“spring boot 2.0.5中的“spring boot split string parameters at comma”行为吗?
【问题讨论】:
-
你为什么不去 POST 并检索正文中的值?
-
POST、PUT、PATCH 用于将数据发布到服务中,在这里您通常使用正文,因为这样您可以利用 SSL 并且发送的数据是加密的。 GET 通常会返回一个正文,但返回的数据会根据您发送的查询参数进行过滤。例如,您在搜索引擎中进行搜索。您想获取根据您搜索“香蕉”的内容过滤的结果。查询字符串包含香蕉,它会在正文中返回我的结果。
标签: spring-boot