gpt4 book ai didi

rest - Thymeleaf 与 Spring MVC + Swagger

转载 作者:行者123 更新时间:2023-12-02 01:02:04 26 4
gpt4 key购买 nike

我想知道如何整合这三种技术。我目前正在为我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com