为仅REST应用程序禁用Spring MVC视图解析

时间:2017-03-09 21:32:41

标签: spring spring-mvc spring-boot

我想为我的应用程序禁用Spring MVC视图解析。

我的应用程序仅将Thymeleaf用于邮件模板,将Spring MVC用于REST API。它不提供Thymleaf页面。

我在application.properties中配置了以下属性:

spring.thymeleaf.enabled=false

但我仍然会遇到如下错误:

20:28:51.851 [http-nio-8080-exec-10] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet dispatcherServlet threw exception
javax.servlet.ServletException: Circular view path [error]: would dispatch back to the current handler URL [/error] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
        at org.springframework.web.servlet.view.InternalResourceView.prepareForRendering(InternalResourceView.java:205)
        at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:145)
        at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
        at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
        at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
        at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:726)
        at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:394)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:311)
        at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:395)
        at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:254)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:177)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:784)
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:802)
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1410)
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)

如何在禁用Spring MVC的视图解析时使用Thymeleaf?

编辑:实际上,我所服务的唯一视图是SPA的index.html,如下所示:

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/").setViewName("forward:/index.html");
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/**")//
            .addResourceLocations("classpath:/")//
            .setCachePeriod(ONE_YEAR);
    registry.addResourceHandler("/")//
            .addResourceLocations("classpath:/index.html")//
            .setCachePeriod(0);
}

2 个答案:

答案 0 :(得分:1)

我认为这里的问题是,当spring遇到错误时,它会将“user”转发到/ error页面。尝试在您的媒体资源中添加以下内容。

spring.resources.add-mappings=false

答案 1 :(得分:0)

您可能有一个控制器,它返回一些无法被 Spring View 解析器解析的字符串。如果您想返回 JSON 对象,请为您的类使用 @RestController 批注

<块引用>

MainController.java

@RestController
public class MainController {

    @GetMapping("/")
    public TestResponseDto greeting(/*here can be any params*/) {
        System.out.println("here");
        return new TestResponseDto();
    }   
}
<块引用>

TestResponse.java

public class TestResponse {
    public String response = "test";
}