对上下文路径/生成405方法的spring引导请求不允许静态内容出错

对上下文路径/生成405方法的spring引导请求不允许静态内容出错,spring,spring-boot,Spring,Spring Boot,我有一个带有上下文路径/服务的spring引导应用程序。属性文件中的上下文路径项: server.servlet.context-path=/services 当我使用URL时http://localhost:8080/services/ 使用GET方法,它工作正常,并返回静态index.html文件作为响应。但是当我尝试使用相同的URL时http://localhost:8080/services/ 使用POST方法,我得到以下错误: { "timestamp":

我有一个带有上下文路径/服务的spring引导应用程序。属性文件中的上下文路径项:

server.servlet.context-path=/services
当我使用URL时http://localhost:8080/services/ 使用GET方法,它工作正常,并返回静态index.html文件作为响应。但是当我尝试使用相同的URL时http://localhost:8080/services/ 使用POST方法,我得到以下错误:

{
    "timestamp": "2020-11-11T07:06:37.341+00:00",
    "status": 405,
    "error": "Method Not Allowed",
    "message": "",
    "path": "/services/"
}
我尝试过使用重定向上下文根属性,但没有帮助。我还尝试了server.servlet.context path=/services/-但这也没有帮助。什么属性或配置将强制spring框架允许POST方法处理任何带有尾随斜杠(/)的上下文路径的请求

从日志中,我可以看到视图正在解析为[view=“forward:index.html”],但紧接着我们看到了错误HttpRequestMethodNotSupportedException:请求方法“POST”不受支持。因此,我们无法理解哪个spring类负责抛出此错误?我们如何才能抑制这种行为

o.s.s.web.util.matcher.OrRequestMatcher  : Trying to match using Ant [pattern='/rest/**']
o.s.s.w.u.matcher.AntPathRequestMatcher  : Checking match of request : '/'; against '/rest/**'
o.s.s.web.util.matcher.OrRequestMatcher  : No matches found
o.s.web.servlet.DispatcherServlet        : POST "/services/", parameters={}
pertySourcedRequestMappingHandlerMapping : looking up handler for path: /
o.s.b.a.w.s.WelcomePageHandlerMapping    : Mapped to ParameterizableViewController [view="forward:index.html"]
o.s.s.w.header.writers.HstsHeaderWriter  : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@363fa5d2
w.c.HttpSessionSecurityContextRepository : SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
o.s.web.servlet.DispatcherServlet        : Completed 405 METHOD_NOT_ALLOWED

报告了一个spring问题:。但spring也明确表示,他们不会做出任何这样的更改,因为对静态资源的请求不应该是POST类型的,因为我们并没有试图修改它们,而是试图获取它们

我正在寻找一种解决方法,因为我必须在上下文根目录下支持POST(因为SSO-IDP要求)。所以我尝试在过滤器中复制302重定向行为。在其中一个过滤器中,我检查了请求方法POST、请求URI/services/并将请求重定向到/services/。这将POST请求转换为GET at path/services/并且可以工作

// Inside doFilter() method of a Filter
if ("POST".equalsIgnoreCase(httpRequest.getMethod()) && "/services/".equalsIgnoreCase(httpRequest.getRequestURI())) {
    httpResponse.sendRedirect("/services/"); // This converts the POST request to GET request
    return;
}

在过滤器中引入此行为后,当我从上述过滤器向发布请求时,它会被重定向到使用GET方法,我会得到所需的index.html作为响应。

您的控制器上有哪些注释<代码>获取映射?事实上,您得到的是405而不是404,这意味着路径是好的,并且您可以到达控制器,您只需告诉它在那里处理POST,就可以了。对于这个上下文路径/路径,没有任何控制器映射。我的所有RestController都有@PostMapping和特定值属性。所以我没有任何显式的/path控制器。