Thymeleaf 与 Spring MVC + Swagger

crm*_*m86 2 rest spring-mvc swagger thymeleaf swagger-ui

我想知道如何整合这三种技术。我目前正在使用 Spring MVC 和 Thymeleaf 模板左轮手枪为我的 Web 视图构建 Web 应用程序。我的 web MVC+T​​hymeleaf 配置完全没有 XML,这里是 web 配置最相关的部分:

@Configuration
@EnableWebMvc
public class ConfigWebMVC extends WebMvcConfigurerAdapter
{
/**
 * Thymeleaf config - Spring will use Thymeleaf to render the HTML views
 * @return The Thymeleaf resolver
 */
@Bean
public ServletContextTemplateResolver templateResolver() {
    LOGGER.info("CREATING TEMPLATE RESOLVER");
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix( environment.getProperty("web.template_prefix") );
    resolver.setSuffix( environment.getProperty("web.template_suffix") );
    resolver.setTemplateMode( environment.getProperty("web.template_style") );
    resolver.setCacheable(true);
    return resolver;
}

/**
 * Thymeleaf config - Thymeleaf will use templateEngine to understand 
 * Spring MVC, Spring Security dialects and th: tags
 * @return The Thymeleaf engine
 */
@Bean 
public SpringTemplateEngine templateEngine() {
    LOGGER.info("CREATING TEMPLATE ENGINE");
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setDialect( new SpringStandardDialect() );
    engine.setTemplateResolver(templateResolver());     
    return engine;
}

/**
 * Thymeleaf config - Spring MVC will use thymeleafViewResolver to set the correct template resolver 
 * @return The Thymeleaf resolver
 */
@Bean 
public ThymeleafViewResolver thymeleafViewResolver() {
    LOGGER.info("CREATING TEMPLATE ENGINE RESOLVER");
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    return resolver;
}
}
Run Code Online (Sandbox Code Playgroud)

此时,我想添加一些休息控制器及其使用 swagger / swagger UI 制作的文档,但我不知道与 thymeleaf 的集成。我知道可以与 Spring MVC 集成,并且我有一个没有 thymeleaf 的工作示例。

使用 swagger 不是强制性的,但我正在寻找一种实用且视觉友好的文档工具来与这些技术集成。

Han*_*akr 5

按照以下步骤将 Swagger 与 thymeleaf 和 spring MVC 集成

1- 将所需的依赖项添加到您的 pom 文件中

<!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

       <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies> 
Run Code Online (Sandbox Code Playgroud)

2- 将 WebMvcConfigurerAdapter 扩展为 addResourceHandlers

@Configuration
@EnableAsync
public class WebConfig extends WebMvcConfigurerAdapter {

    public WebConfig() {
        super();
    }

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
Run Code Online (Sandbox Code Playgroud)

3-创建招摇配置文件

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.ant("/**")).build().apiInfo(apiInfo()).useDefaultResponseMessages(false);

    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("Application name", "API description", "API TOS", "Terms of service", new Contact("Hany Sakr", "website", "email"), "License of API", "API license URL", Collections.emptyList());
        return apiInfo;
    }
}
Run Code Online (Sandbox Code Playgroud)

4-运行应用程序后,使用以下链接访问 swagger

http://localhost:8080/{YourApplication}/v2/api-docs
http://localhost:8080/{YourApplication}/swagger-ui.html
Run Code Online (Sandbox Code Playgroud)

我希望它能有所帮助。