Spring MVC 集成 Swagger,API文档自动生成~

废话不多说,先上一些资料

Swagger API


这里,我的Spring MVC 版本是4.2.RELEASE

swagger maven依赖

<dependency>
			<groupId>com.mangofactory</groupId>
			<artifactId>swagger-springmvc</artifactId>
			<version>1.0.2</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.4.2</version>
		</dependency>


新建一个文件,用做扩展的Swagger配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;


import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

@Configuration
@EnableSwagger
@ComponentScan(value = { "hy.cmcc.oa.api.controller", "hy.cmcc.oa.web.controller" })   //这个不加,貌似也没关系
public class SwaggerConfig {
    private SpringSwaggerConfig springSwaggerConfig;

    @Autowired
    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {
        this.springSwaggerConfig = springSwaggerConfig;
    }

    @Bean
    public SwaggerSpringMvcPlugin customImplementation() {
        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo())
                .includePatterns(".*").apiVersion("0.0.1");
        //.swaggerGroup(PROJECT_NAME);
    }

    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfo("开发文档", "API文档", "BaseURL des",
                "contact email", "Project License", "Project API License URL");
        return apiInfo;
    }
}


Spring-mvc.xml 配置扫描上面新建的新建文件路径

<context:component-scan base-package="hy.cmcc.oa.web.base.swagger" />
顺便配置对静态资源的访问,我这里是开启了全局的静态资源访问权限,有具体需求的可以针对的修改

<mvc:default-servlet-handler />
或者
<mvc:resources mapping="*.html" location="/" />

接下来就是通过swagger的ui组件对API接口可视化展示,网上的很多教程都是在 GitHub上下载dist文件夹下的资源,但是我发现集成起来没有效果,于是通过了另一种办法,下载 Spring-swagger-ui-2.4.0.jar,解压开


获取这个jar里面的资源文件,在webapp下新建一个swagger文件夹,把这些资源文件丢进去


然后编辑index.html


将url替换为自己的api路径

http://ip:port/projectName/api-docs

启动tomcat,访问   http://127.0.0.1:8081/oa/swagger/index.html  即可,注意url的ip要和浏览器的访问ip保持一致,否则Ajax会存在跨域而无法访问的问题,效果如下




注意,@RequestMapping的配置,不仅要有value,还要有method声明

不然可能会出现这种情况



其他的注解使用情况可以参考

Controller注解

@Api(description = "功能模块设置", value = "/moulde", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)


Method注解

@ApiOperation(value = "list", notes = "获取结构树数据", httpMethod = "GET", response = JsonResult.class, position = 1)

@ApiIgnore声明该Controller或者Method被Swagger忽略


更多的注解使用可以参考官方API

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用 Springfox 来集成 Swagger 和 Knife4j。Springfox 是一个用于构建 Swagger UI 和 Knife4j UI 的库。可以通过简单的配置,将 Swagger 和 Knife4j 集成Spring Boot 项目中。 ### 回答2: Spring Boot 集成 Swagger 和 Knife4j 的步骤如下: 1. 在项目的 pom.xml 文件中添加相关依赖: ```xml <!-- Swagger2 依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <!-- Knife4j 依赖 --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.1</version> </dependency> ``` 2. 在项目的启动类上增加注解 `@EnableSwagger2`。 ```java @SpringBootApplication @EnableSwagger2 public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 3. 配置 Swagger 相关参数,创建一个配置类(如 SwaggerConfig)。 ```java @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API 文档") .description("示例项目的API文档") .version("1.0.0") .build(); } } ``` 4. 在浏览器中访问 Swagger 文档,URL 一般为 `http://localhost:8080/swagger-ui.html`,通过 Swagger 可以查看和测试 API 接口。 5. 如需使用 Knife4j 的增强功能,修改 Swagger 配置类中的 `Docket` 为 `Knife4jDocket`。 ```java @Configuration public class SwaggerConfig { @Bean public Knife4jDocket knife4jDocket() { return new Knife4jDocket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API 文档") .description("示例项目的API文档") .version("1.0.0") .build(); } } ``` 经过以上步骤,就可以成功集成 Swagger 和 Knife4j,通过 Swagger 可以方便地查看和测试 API 接口文档,同时 Knife4j 提供了更多的增强功能,可以更好地管理和展示 API 接口文档。 ### 回答3: 在Spring Boot中集成Swagger和Knife4j的步骤如下: 1. 添加依赖: 在Maven的pom.xml文件中,添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> ``` 2. 创建Swagger配置类: 创建一个Swagger配置类,用于配置Swagger的相关参数。可以参考以下示例代码: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档") .description("API文档") .version("1.0") .build(); } } ``` 3. 配置Knife4j的扩展设置: 创建一个Knife4j配置类,用于配置Knife4j的相关参数。可以参考以下示例代码: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; @Configuration @EnableSwagger2WebMvc public class Knife4jConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("doc.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Bean public Knife4jProperties knife4jProperties() { Knife4jProperties properties = new Knife4jProperties(); properties.setEnableKnife4j(true); return properties; } } ``` 4. 启动应用程序: 启动Spring Boot应用程序,并访问"http://localhost:8080/swagger-ui.html",即可查看生成的Swagger API文档。 总结: 通过添加相关的依赖和配置类,我们可以很容易地在Spring Boot中集成Swagger和Knife4j。Swagger用于生成API文档,而Knife4j是对Swagger的扩展,提供了更丰富的功能和界面。集成后,我们可以方便地查看和测试接口文档
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值