rest - Thymeleaf 与 Spring MVC + Swagger

标签 rest spring-mvc thymeleaf swagger swagger-ui

我想知道如何整合这三种技术。 我目前正在为我的 Web View 使用 Spring MVC 和 Thymeleaf 模板左轮手枪构建 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;
}
}

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

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

最佳答案

按照这些步骤将 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> 

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

3- 创建 swagger 配置文件

@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;
    }
}

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

http://localhost:8080/{YourApplication}/v2/api-docs
http://localhost:8080/{YourApplication}/swagger-ui.html

希望对您有所帮助。

关于rest - Thymeleaf 与 Spring MVC + Swagger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27430127/

相关文章:

angularjs - Keycloak:在 AngularJS 应用程序中更新用户密码

rest - 在 GET URL 中指定 OAuth token

node.js - Azure 图形 API 在 Node JS 中使用自定义用户属性创建 B2C 用户

java - 为什么我的 REST API 在嵌套对象上返回空白 JSON?

java - 通过Java Spring注解替换字符串

java - 如何在 Thymeleaf 中执行 if-else?

spring - 使用 spring-boot 创建企业 Maven 应用程序

java - Spring Security 多个登录用户失败

spring-boot - if-unless 在 thymeleaf 引擎中给出相同的返回

java - Thymeleaf - 如何添加自定义实用程序?