Spring 将 @GetMapping 引导至 URL 以 .jsp 结尾

Spring Boot @GetMapping to a URL ending with .jsp

我们有一个 URL 必须以(客户要求)结尾,例如 /test.jsp。

现在我们终于 运行ning Spring 启动 (2.1.1.RELEASE),我们想放弃 JSP 并使用一些模板引擎,在这种情况 小胡子。我有一个这样的控制器映射:

@Controller
@RequestMapping("/")
public class TestController  {

    @GetMapping(path = "/test.jsp")
    public ModelAndView test(...) {...}
}

这根本行不通。我有

spring.mvc.pathmatch.use-suffix-pattern=true

在我们的 application.properties 中,任何与 spring.mvc.view 相关的内容都被注释掉了,当我添加另一个仅包含 /test 的映射时,它适用于 /test。有趣的是,在使用 Spring MVC 和 Thymeleaf 时,我设法得到了完全相同的东西,但我似乎找不到区别。

此外,我为此添加了一个测试,如下所示:

@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
class TestTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void test() throws Exception {
        final MockHttpServletRequestBuilder testRequestBuilder = MockMvcRequestBuilders.get("/test.jsp");

        MvcResult responseResult = mockMvc.perform(testRequestBuilder).andReturn();


        response = responseResult.getResponse();
        assertThat(response.getStatus(), equalTo(HttpStatus.OK.value()));
    }

}

这个很好用,回复的内容也正是我想要的。测试配置文件与使用 mvn spring-boot:运行 时的配置文件相同。

有人知道如何让它工作吗?谢谢!

圆点可能被截断了。或逃脱。 你或许可以这样做:

@GetMapping("/{pageName:.+}")   
public void test(
  @PathVariable("pageName") String pageName) {
     if(pageName.equals("test.jsp")) {
                   //...
}

我知道你并不完全想要一个变量,只是抛出一个想法

我终于解决了 - 我的一切正常,包括 test.html、test.xml、test.wasd 和测试。所以我认为它本身不可能是 Spring。在各种 Tomcat 类 中进行一些调试后,我发现了罪魁祸首:JspServlet 以某种方式存在于类路径中并被自动配置,源自

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

删除依赖导致 test.jsp 被正确识别。