1. 配置文件 pom, 一定要使用 2.4 以上的, 2.4 默认请求方式是 JSON, 会导致 getmapping 设置参数类型对对象时, swaggerui 界面不能指定为其他类型, 反正就是各种坑, 不建议用
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.8.0</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.8.0</version>
- </dependency>
2. 使 spring 管理 swaggerui 配置
- package org.liping.springribbon;
- 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.service.Contact;
- import springfox.documentation.spi.DocumentationType;
- import springfox.documentation.spring.web.plugins.Docket;
- import springfox.documentation.swagger2.annotations.EnableSwagger2;
- @Configuration
- @EnableSwagger2
- public class Swagger2 {
- //swagger2 的配置文件, 这里可以配置 swagger2 的一些基本的内容, 比如扫描的包等等
- @Bean
- public Docket createRestApi() {
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select()
- // 为当前包路径
- .apis(RequestHandlerSelectors.basePackage("org.liping.springribbon"))
- .paths(PathSelectors.any())
- .build();
- }
- // 构建 API 文档的详细信息函数, 注意这里的注解引用的是哪个
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- // 页面标题
- .title("Spring Boot Swagger2 ui")
- // 创建人
- .contact(new Contact("liping1", "https://home.cnblogs.com/u/keepMoveForevery/", ""))
- // 版本号
- .version("1.0")
- // 描述
- .description("API 描述")
- .build();
- }
- }
3. 在自己的 Controler 加入 swagger-ui 注解开启测试
- package org.liping.springribbon;
- import io.swagger.annotations.API;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.Web.bind.annotation.GetMapping;
- import org.springframework.Web.bind.annotation.RequestMapping;
- import org.springframework.Web.bind.annotation.RestController;
- @RestController
- @RequestMapping(value = "/",produces = "application/json;charset=UTF-8")
- @API(value="用户 controller",description="用户操作",tags={"用户操作接口"})
- public class HelloControler {
- @Autowired
- HelloService helloService;
- @ApiOperation(value = "hi 接口名字", notes = "这是一个接口的描述")
- @GetMapping(value = "/hi")
- public UserModel hi(String name, int id){
- UserModel userModel = new UserModel();
- userModel.setId(id);
- userModel.setName(name);
- System.out.println("他被调用了");
- return userModel;
- }
- @ApiOperation(value = "hi1 接口名字")
- @RequestMapping(value = "/hi1")
- public UserModel hi1(UserModel userModel){
- UserModel userModel1 = new UserModel();
- userModel1 = userModel;
- System.out.println("他被调用了 1"+ userModel.getId());
- return userModel1;
- }
- }
下面是我用来做测试测对象代码
- package org.liping.springribbon;
- import io.swagger.annotations.ApiModelProperty;
- public class UserModel {
- @ApiModelProperty(value="id",name="id",required=true)
- private int id;
- private String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "UserModel{" +
- "id=" + id +
- ", name='" + name + '\'' +
- '}';
- }
- }
就这么简单, 其他一些属性自行百度
下面是代码结构以及执行结果
最后是代码结构
来源: http://www.bubuko.com/infodetail-2996628.html