Spring WebFlux无法在Tomcat中运行

时间:2017-10-19 14:44:33

标签: java spring spring-mvc tomcat intellij-idea

IntelliJ IDEA作为一个开发工具,我想使用Spring WebFlux开发一个简单的博客系统,我目前使用如下:

  1. Springs FrameWork 5.0;
  2. Thymeleaf
  3. Spring WebFlux;
  4. 我暂时不使用数据库(直到我可以通过Spring WebFlux访问我的主页)。在Spring WebFlux中,我使用了Java Config,配置文件如下:

    // WebConfig.java
    @Configuration
    @EnableWebFlux
    public class WebConfig implements WebFluxConfigurer {
    
    
    @Override
    public void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) {
        builder.fixedResolver(MediaType.ALL);
    }
    
    
    
    
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(viewResolver());
    }
    
    
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(org.springframework.web.context.ContextLoader.getCurrentWebApplicationContext());
        templateResolver.setPrefix("/resources/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCacheable(true);
        return templateResolver;
    }
    
    
    public SpringWebFluxTemplateEngine templateEngine() {
        SpringWebFluxTemplateEngine templateEngine = new SpringWebFluxTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }
    
    
    public ThymeleafReactiveViewResolver viewResolver() {
        ThymeleafReactiveViewResolver viewResolver = new ThymeleafReactiveViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setOrder(1);
        return viewResolver;
    }
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations( "classpath:/src/main/resources/static/images/");
    }
    
    
    }
    

    另一个配置类:

    @Configuration
    @ComponentScan("xyz.suancaiyu")
    public class RootConfig extends 
        AbstractAnnotationConfigDispatcherHandlerInitializer {
    
        @Override
        protected Class<?>[] getConfigClasses() {
            return new Class[]{WebConfig.class};
        }
    }
    

    控制器:

    @Controller
    public class HomeController {
    
        @RequestMapping("/")
        public String index() {
            String home = "hello";
            return "home";
        }
    }
    

    HTML:

    &#13;
    &#13;
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <p th:text="${home}"></p>
    </body>
    </html>
    &#13;
    &#13;
    &#13;

    但是当我提示500个错误时,我访问了http://localhost:8080,但我看不到IntelliJ IDEA中的特定错误。

    请帮帮我,谢谢。

    所有代码,您都可以查看:https: //github.com/dcxy0/Spring5WebFluxDemo

1 个答案:

答案 0 :(得分:0)

它可以帮助http://andrunevchyn.com/2017/10/27/spring-5-webflux-without-springboot/ 这取决于您的部署方式。如果将其部署为war文件,我建议尝试使用项目名称调用url。另请检查您的webxml配置。