Springboot 整合 Swagger
612人浏览 / 0人评论
参考:
参数类型 dataType 设置:https://blog.csdn.net/qq_39393671/article/details/103307690
依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.22</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @Author 风仔
* @Date 2022/6/23
* @Version 1.0
* @Description:
**/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.basePackage("com.jbritian.springdatajpa.web.rest")) // 控制层路径
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("SpringBoot整合Swagger")
.description("SpringBoot整合Swagger,详细信息......")
.version("1.0")
.contact(new Contact("啊啊啊啊","blog.csdn.net","aaa@gmail.com"))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build());
}
}
SwaggerResource.java
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author 风仔
* @Date 2022/6/23
* @Version 1.0
* @Description:
**/
@RestController
@Api(tags = "swagger测试接口")
public class SwaggerResource {
@ApiOperation(value = "根据用户id查询用户信息", notes = "查询用户信息")
@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "Int")
@GetMapping(value = "/findById")
public Integer findById(@RequestParam(value = "id") Integer id) {
System.err.println(id);
return id;
}
@ApiOperation(value = "查询所有信息", notes = "查询数据库中某个学生的信息")
@GetMapping(value = "/queryAll")
public List<String> queryAll() {
List<String> data = new ArrayList<>();
data.add("java134");
data.add("java资料社区");
return data;
}
@ApiOperation(value = "获取redis中数据", notes = "通过key值获取")
@ApiResponses({
@ApiResponse(code = 400, message = "请求报文存在语法错误"),
@ApiResponse(code = 401, message = "发送的请求需要有通过 HTTP 认证的认证信息"),
@ApiResponse(code = 403, message = "请求资源的访问被服务器拒绝"),
@ApiResponse(code = 404, message = "服务器上没有找到请求的资源")
})
@ApiImplicitParam(name = "key", value = "redis键值", required = true, dataType = "String")
@PostMapping(value = "/getRedis")
public String getRedis(String key) {
Map<String, String> data = new HashMap<>();
data.put("123", "Java资料社区");
data.put("321", "Java");
data.put("132", "小编");
return data.get(key);
}
@ApiIgnore
@RequestMapping("/index")
public String index() {
return "ApiIgnore=忽略这个接口";
}
}
启动后在浏览器输入:IP:端口/swagger-ui.html
全部评论