使用Spring Boot在嵌入式Tomcat服务器设置上的Spring MVC应用程序的路径变量中的非ASCII符号 [英] Non-ASCII symbols in path variable of Spring MVC application on embedded Tomcat server setup using Spring Boot
问题描述
我正在使用Spring MVC构建一个服务,该服务是通过Spring Boot设置的,我希望在URL中包含任意unicode字符.
I am building a service using Spring MVC set up using Spring Boot where I want to be able to have arbitrary unicode characters in the URLs.
通过环顾网上,我最终得到了
By looking around the web I end up with
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Main {
public static void main(String... args) throws Exception {
SpringApplication.run(Main.class, args);
}
@Bean
public Filter characterEncodingFilter() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return characterEncodingFilter;
}
}
和
@Controller
public class WordController {
@RequestMapping(value="/word/{word}", method=RequestMethod.GET)
public String greeting(Model model, @PathVariable("word") String word) {
System.out.println(word);
model.addAttribute("word", word);
return "word";
}
}
其中的模板单词"只是从模型中打印出单词.
where the template "word" just prints out the word from the model.
当我启动服务器并在Chrome中输入http://localhost:8080/word/æøå
时,响应页面(和终端上)上打印的文本是
When I start the server and enter http://localhost:8080/word/æøå
into Chrome, the text printed on the response page (and in the terminal) is
æøå
当我将丹麦字母æøå
实际上以UTF-8编码时,我认为我将其理解为ISO-8859-1解释.
which I think I recognize as a ISO-8859-1 interpretation the Danish letters æøå
when they're actually encoded in UTF-8.
查看Chrome的网络检查器,我发现它实际上是在查询http://localhost:8080/word/%C3%A6%C3%B8%C3%A52
,实际上它似乎是UTF-8中字符串的URL编码.
Looking into Chrome's net inspector I see that it actually queries http://localhost:8080/word/%C3%A6%C3%B8%C3%A52
which indeed seems to be the URL encoding of the string in UTF-8.
任何人都可以解释为什么尽管进行了这种配置,但Spring仍无法将路径变量解析为UTF-8,以及如何实现?
Can anyone explain why Spring doesn't parse the path variable as UTF-8 despite this configuration, and how to make it?
对于CharacterEncodingFilter
是否真正解决了这个问题,似乎存在不同意见.至少在我的另一个(非引导)Spring项目中,我使用web.xml
注册CharacterEncodingFilter
.在这里,它已成功地用于使POST正文解析为UTF-8,但我也不能使其用于路径变量.
There seems to mixed opinions on whether CharacterEncodingFilter
actually solves this problem. At least, in another (non-Boot) Spring project of mine I use web.xml
to register the CharacterEncodingFilter
. Here it's used successfully to make POST bodies parse as UTF-8, but I coudn't make it work for path variables there either.
此答案建议应在Tomcat的设置中对其进行配置.如果是这样,那么如何在嵌入式服务器上完成该操作?
This answer suggests that it should be configured in Tomcat's settings. If so, how is that done on an embedded server?
推荐答案
在发生脑波事件之后,添加bean方法
Following the event of a brain-wave, adding the bean method
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(8080);
factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
@Override
public void customize(Connector connector) {
connector.setURIEncoding("UTF-8");
}
});
return factory;
}
似乎可以解决问题.
修改
CharacterEncodingFilter
对于转换POST正文仍然是必需的.
The CharacterEncodingFilter
is still necessary for converting POST bodies.
这篇关于使用Spring Boot在嵌入式Tomcat服务器设置上的Spring MVC应用程序的路径变量中的非ASCII符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!