pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 网站依赖http://localhost:9090/swagger-ui.html-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
application.yml
server:
port: 9090
swagger:
enabled: true
SwaggerApplication.java
package com.mingshine.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2//表示开启Swagger
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}
User.java
package com.mingshine.cloud.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
@ApiModel("用户实体类") //字段比较多就注释
@Data
public class User {
@ApiModelProperty("用户id") //注释
private int id;
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("年龄")
private int age;
@ApiModelProperty("时间")
private Date ctm;
}
Swagger2Config.java
package com.mingshine.cloud.config;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration //配置类
public class Swagger2Config {
/**
* com.mingshine.cloud.controller包的路径,不然生成的文档扫描不到接口
*
* @return
*/
@Bean
Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.mingshine.cloud.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(new ApiInfoBuilder()
.description("描述:测试接口..测试接口测试接口测试接口测试接口测试接口测试接口测试接口测试接口测试接口.")
.title("标题:明耀_测试系统_接口文档")
.version("V1.0.0")
.contact(new Contact("明耀", "335415534@qq.com", "335415534@qq.com"))
.build());
}
}
UserController.java
package com.mingshine.cloud.controller;
import com.mingshine.cloud.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
@RestController
@Api(tags="用户接口") //相当于类注释
public class UserController {
@GetMapping("/hello")
public String hello(){
return "heeo swagger测试";
}
@GetMapping("/user")
@ApiOperation("根据用户id查询") //相当于方法注释
@ApiImplicitParam(name = "查询id",value = "用户id",defaultValue = "99") // 相当方法的参数注释
public User getUserId(Integer id){
User user = new User();
user.setId(id);
return user;
}
@PutMapping("/user")
@ApiOperation("根据id更新用户名")
@ApiImplicitParams({ //这注解能写多个参数注释
@ApiImplicitParam(name = "id",value = "用户id",defaultValue = "99"),
@ApiImplicitParam(name = "username",value="用户名",defaultValue = "默认值")
})
public User updateUsernameById(String username,Integer id){
User user = new User();
user.setId(id);
user.setUsername(username);
return user;
}
@DeleteMapping("/user{id}")
@ApiOperation("根据id删除用户")
@ApiImplicitParam(name = "id",value = "用户id",defaultValue = "99")
public void deleteUserById(@PathVariable Integer id){
System.out.println(id);
}
@PostMapping("/user")
@ApiOperation("根据对象添加用户")
public User addUser(@RequestBody User user){
return user;
}
}
运行访问地址:
http://localhost:9090/swagger-ui.html#/