春季启动-嵌入式Tomcat-连接器定制程序-无法添加parseBodyMethods属性 [英] Spring boot - Embedded Tomcat - Connector Customizer - fail to add parseBodyMethods attributes
问题描述
最初的问题是,当我使用方法"DELETE"发送http请求时,正文部分无法发送到服务器.
The original problem is when I sent a http request with method 'DELETE', the body part couldn't be sent to the server.
使用Google搜索后,我发现这篇文章建议修改服务器. xml文件并将"parseBodyMethods"添加到连接器部分可以解决此问题:
After googling, I found this article that suggests modifying the server.xml file and adding 'parseBodyMethods' to the Connector part can solve the problem:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
parseBodyMethods="POST,PUT,DELETE"
redirectPort="8443" />
但是,因为我正在使用spring的嵌入式tomcat,所以我必须找到一种以spring的方式进行相同操作的方法.因此,我发现这篇文章似乎可以让我添加ConnectorCustomizer并向其中添加其他属性连接器.以下是我的代码:
However, because I'm using spring's embedded tomcat, I have to find a way to do the same in spring's way. So, I found this article that seems to allow me to add ConnectorCustomizer and add additional attribute to the Connector. The following is my code:
public class MyTomcatConnectorCustomizer implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer factory) {
if(factory instanceof TomcatEmbeddedServletContainerFactory) {
customizeTomcat((TomcatEmbeddedServletContainerFactory) factory);
}
}
public void customizeTomcat(TomcatEmbeddedServletContainerFactory factory) {
TomcatEmbeddedServletContainerFactory tomcat = (TomcatEmbeddedServletContainerFactory) factory;
tomcat.addConnectorCustomizers(connector -> {
connector.setAttribute("parseBodyMethods", "POST,PUT,DELETE");
});
}
}
@Bean
MyTomcatConnectorCustomizer myTomcatConnectorCustomizer() {
MyTomcatConnectorCustomizer myTomcatConnectorCustomizer = new MyTomcatConnectorCustomizer();
return myTomcatConnectorCustomizer;
}
但是,仍然存在相同的问题.当我向服务器发送删除"请求时,正文仍然是空的.有人遇到过同样的问题吗?感谢帮助!
But still, the same issue exists. the body is still empty when I send a 'DELETE' request to the server. Does anyone have encountered the same issue before? Help appreciated!
推荐答案
更改
connector.setAttribute("parseBodyMethods", "POST,PUT,DELETE");
到
connector.setParseBodyMethods("POST,PUT,DELETE")
或者只是
@Bean
public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory() {
return new TomcatEmbeddedServletContainerFactory(){
@Override
protected void customizeConnector(Connector connector) {
super.customizeConnector(connector);
connector.setParseBodyMethods("POST,PUT,DELETE");
}
};
}
这篇关于春季启动-嵌入式Tomcat-连接器定制程序-无法添加parseBodyMethods属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!