SpringBoot之结合Swagger2这个测试api

1.声明

当前内容主要用于复习和使用SpringBoot中操作Swagger2这个测试api

2.pom依赖

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.18.RELEASE</version>
</parent>
<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger2</artifactId>
		<version>2.8.0</version>
	</dependency>
	<dependency>
		<groupId>io.springfox</groupId>
		<artifactId>springfox-swagger-ui</artifactId>
		<version>2.8.0</version>
	</dependency>
</dependencies>

3.demo

1.最重要的配置类:Swagger2Config.java

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2 // 开启Swagger2
public class Swagger2Config {
    
    
	@Bean
	public Docket restApi() {
    
    
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
				// 是否开启
				.enable(true)// 正式环境中为false,true表示测试、生产环境中
				.select()
				// 扫描的路径包
				.apis(RequestHandlerSelectors.basePackage("com.hy.springboot.swagger2"))// 表示是swagger扫描api的包位置
				// 指定路径处理PathSelectors.any()代表所有的路径
				.paths(PathSelectors.any()).build().pathMapping("/");
	}
	
	public ApiInfo apiInfo() {
    
    
		return new ApiInfoBuilder().title("Swagger2 api")
				.description("用户管理专门测试api")
				.contact(new Contact("username","url","email"))
				.version("1.1v")
				.build();
	}
}

主要添加需要使用Swagger API的包

2.基本实体类:User.java

import java.util.Date;
import org.springframework.format.annotation.DateTimeFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel
public class User {
    
    
	@ApiModelProperty(value="用户id编号",dataType="Integer",name="id",example="1")
	private Integer id;
	@ApiModelProperty(name = "now",dataType = "Date", value = "现在的时间",example = "2020-10-10 10:10:10")
	@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date now;

	public Integer getId() {
    
    
		return id;
	}

	public void setId(Integer id) {
    
    
		this.id = id;
	}

	public Date getNow() {
    
    
		return now;
	}

	public void setNow(Date now) {
    
    
		this.now = now;
	}

	public User(Integer id) {
    
    
		super();
		this.id = id;
		this.now = new Date();
	}

	public User() {
    
    
		super();
		this.now = new Date();
	}

	@Override
	public String toString() {
    
    
		return "User [id=" + id + ", now=" + now + "]";
	}

}

3.主要controller:MainController.java

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.hy.springboot.swagger2.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@RestController
@Api(tags = "MainController")
public class MainController {
    
    

	@ApiOperation(value = "根据用户id编号查询用户")
	@ApiImplicitParam(name = "id", value = "查询ID", example = "1", dataTypeClass = Integer.class, required = true)
	@RequestMapping(value = "/getUserById/{id}", method = RequestMethod.GET)
	public User getUserById(@PathVariable Integer id) {
    
    
		return new User(id);
	}

	@ApiOperation(value = "根据用户id编号删除用户")
	@ApiImplicitParam(name = "id", value = "删除用户ID", example = "1", dataTypeClass = Integer.class, required = true)
	@RequestMapping(value = "/deleteUserById", method = RequestMethod.GET)
	public String deleteUserById(Integer id) {
    
    
		return "删除用户成功!";
	}

	@ApiOperation(value = "添加用户信息")
	// @ApiImplicitParam(name = "",dataTypeClass = User.class,value = "添加的用户",
	// required = true)
	// 所以传递对象的时候,请求根本不需要写@ApiImplicitParam,否则导致页面问题
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public User addUser(User user) {
    
    
		return new User(user.getId());
	}
}

这里需要小心当前的传递数据为对象的时候的@ApiImplicitParam并不需要写,如果写后就会出现问题

4.基本入口类

/**
 * @author admin
 * @createTime 2021-03-01 11:30:07
 * @description 当前内容主要在SpringBoot中使用Swagger2
 * 
 */
@SpringBootApplication
public class SpringBootSwagger2App 
{
    
    
    public static void main( String[] args )
    {
    
    
       SpringApplication.run(SpringBootSwagger2App.class, args);
    }
}

4.基本测试

直接访问:http://localhost:8080/swagger-ui.html,方式访问当前的Swagger2 API接口

在这里插入图片描述

其中Model就是添加APi后的实体类模型@ApiModel,MainController就是@Api(tags = "MainController")

开始测试,点击MainController:
在这里插入图片描述
可以一个一个的测试!

5.总结

1.Swagger2是一个使用注解方式的api访问测试的东西,主要方便测试之用

猜你喜欢

转载自blog.csdn.net/weixin_45492007/article/details/114259172