首先引入依赖信息,
<!-- https://mvnrepository.com/artifact/com.spring4all/swagger-spring-boot-starter -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.8.0.RELEASE</version>
</dependency>
这里我查了maven依赖库,Swagger2-spring-boot-starter的依赖信息包似乎挂到了GitHub上,而且使用率比较低,所以此处选用com.spring4all的类库,这里面整合的也是Swagger2.
编写application.yml文件:
server:
port: 8080
swagger:
enabled: true
print-init: true
docket:
base-package: com.meiszl.Swagger2SpringBoot
api-info:
contact:
name: meiszl
email: tiaode@qq.com
url: https://blog.csdn.net/qq_31404603
编写对应的启动类:
package com.meiszl.Swagger2SpringBoot;
import com.meiszl.Swagger2SpringBoot.Pojo.User;
import io.swagger.annotations.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
@RestController
@Api
@ApiResponses({@ApiResponse(code = 200, message = "success", response = ResponseEntity.class)})
public class SwaggerMain {
public static void main(String[] args){
SpringApplication.run(SwaggerMain.class,args);
}
@GetMapping("/index")
@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
public String index(User User) {
return "index";
}
@GetMapping("/home")
public String home() {
return "home";
}
@GetMapping("/home/test")
public String homeTest() {
return "test";
}
@GetMapping("/test")
public String test() {
return "test";
}
@GetMapping("/index/test")
public String indexTest() {
return "test";
}
@GetMapping("/index/test/a")
public String indexTestA() {
return "test";
}
}
启动访问http://localhost:8080/swagger-ui.html
访问成功