Spring Boot @GetMapping到以.jsp结尾的URL

问题描述 投票:1回答:2

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

现在我们终于运行了Spring Boot(2.1.1.RELEASE),我们想抛弃JSP并使用一些模板引擎,在本例中是Mustache。我有一个像这样的控制器映射:

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

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

这不行。我有

spring.mvc.pathmatch.use后缀图案=真

在我们的application.properties中,任何spring.mvc.view-related都被注释掉了,当我用/ 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时的配置文件相同:暂时运行。

有人知道如何让这个工作?谢谢!

java spring spring-boot mustache
2个回答
0
投票

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

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

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


0
投票

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.