JavaEar 专注于收集分享传播有价值的技术资料

Spring WebFlux can not run in Tomcat

IntelliJ IDEA As a development tool, I would like to develop a simple blog system using Spring WebFlux, which I currently use as follows:

  1. Springs FrameWork 5.0;
  2. Thymeleaf
  3. Spring WebFlux;

I do not use the database for the time being (until I can access my home page via Spring WebFlux). In Spring WebFlux, I used Java Config and the configuration file is as follows:

// 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/");
}


}

Another configuration class:

@Configuration
@ComponentScan("xyz.suancaiyu")
public class RootConfig extends 
    AbstractAnnotationConfigDispatcherHandlerInitializer {

    @Override
    protected Class<?>[] getConfigClasses() {
        return new Class[]{WebConfig.class};
    }
}

Controller:

@Controller
public class HomeController {

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

HTML:

<!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>

But I visited: http://localhost:8080 when prompted 500 errors, but I can not see the specific error in IntelliJ IDEA.

Please help me, thank you.

All the code, you can view: https: //github.com/dcxy0/Spring5WebFluxDemo

1个回答

    最佳答案