欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

eclipse 搭建 swagger-ui(maven项目 springboot框架)

程序员文章站 2022-07-12 13:34:24
...

现在越来越流行swagger,现在把原来的Maven的项目加入swagger

其实非常简单 

1 pom.xml    引入两个jar就可以

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

2 SpringBootApp.java  添加注解 @EnableSwagger2

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
public class App {
	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

3 启动项目 访问 http://localhost:8080/swagger-ui.html

eclipse 搭建 swagger-ui(maven项目 springboot框架)

最基本的swagger就出来了

4 swaggerConfig.java 配置文件(这样页面中出现的东西我们就可以更改了)

SpringBoot的启动ApplicationApp.java同目录下创建java文件 swaggerConfig.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 createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.groupName("controller")
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.pusamm"))//项目包名
				.paths(PathSelectors.any()).build();
	}


	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("移动C2M RESTful API")
				.description("swagger-ui")
				.termsOfServiceUrl("").version("1.0").build();
	}
eclipse 搭建 swagger-ui(maven项目 springboot框架)

页面就编程这样了,其他的根据自己项目需求修改就可以了