天天看點

用swagger2生成後端接口文檔如果覺得文檔界面不是自己喜歡的可以換一種顯示方式

Maven引入

            io.springfox

            springfox-swagger2

            2.9.2

            io.springfox

            springfox-swagger-ui

            2.9.2

建立SwaggerConfig用于配置swagger

用swagger2生成後端接口文檔如果覺得文檔界面不是自己喜歡的可以換一種顯示方式

@Configuration

@EnableSwagger2

public class SwaggerConfig {

public Docket getDocket(){

ApiInfoBuilder apiInfoBuilder=new ApiInfoBuilder();

apiInfoBuilder.title("《xxx系統》後端接口說明")

.description("此文檔說明了xxx系統項目後端接口規範……")

.version("v 2.0.1")

.contact(new Contact("BruceWong","www.brucejkd.com","[email protected]"));

ApiInfo apiInfo =apiInfoBuilder.build();

Docket docket =new Docket(DocumentationType.SWAGGER_2);

docket.apiInfo(apiInfo)                                                                                //指定生成文檔中的封面資訊

                .select()

.apis(RequestHandlerSelectors.basePackage("com.bruce.controller"))    //哪個包下面的類需要生成文檔

                .paths(PathSelectors.any())                                                       //包下面的所有類都生成文檔

//                .paths(PathSelectors.regex("/user/"))                                      //包下面的 帶有“/user/” uri的請求類,生成文檔

                .build();

return docket;

}

}

給VO與實體類加swagger注釋内容

@Data

@AllArgsConstructor

@NoArgsConstructor

@ToString

@ApiModel(value ="User對象", description ="使用者/買家資訊")

public class User {

@ApiModelProperty(dataType ="long", required =false)

private long userId;

@ApiModelProperty(dataType ="String", required =true, value ="使用者名")

private String userName;

@ApiModelProperty(dataType ="String", required =true, value ="使用者密碼")

private String userPassword;

@ApiModelProperty(dataType ="String", required =true, value ="使用者真是姓名")

private String userRealName;

@ApiModelProperty(dataType ="String", required =true, value ="使用者頭像url")

private String userImg;

@ApiModelProperty(dataType ="int", required =true, value ="使用者年齡")

private int userAge;

}

@Data

@NoArgsConstructor

@AllArgsConstructor

@ToString

@ApiModel(value ="響應的VO對象",description ="封裝接口傳回給前端的資料")

public class ResultVO {

@ApiModelProperty("響應狀态碼")

private int code;

@ApiModelProperty("響應提示資訊")

private String msg;

@ApiModelProperty("響應資料内容")

private Object data;

}

在Controller中添加swagger注釋内容

@Controller

@RequestMapping("/user")

@ResponseBody

@Api(value ="提供使用者管理增删改查相關接口", tags ="使用者管理")

public class UserController {

@Autowired

    private UserService userService;

@ApiOperation("使用者注冊接口")

@ApiImplicitParam(name ="user", value ="使用者注冊資訊", required =true)

@PostMapping("/register")

public User register(@RequestBody User user) {

userService.register(user);

return user;

}

@ApiOperation("使用者登入接口")

@ApiImplicitParams({

@ApiImplicitParam(dataType ="string", name ="username", value ="使用者登入賬号", required =true),

@ApiImplicitParam(dataType ="string", name ="password", value ="使用者登入密碼", required =false, defaultValue ="111111")

})

@PostMapping("/checkLogin")

public ResultVO checkLogin(@RequestParam("username")String name,

@RequestParam(value ="password", defaultValue ="111111")String pwd) {

return userService.checkLogin(name, pwd);

}

}

運作檢視文檔

http://localhost:8080/swagger-ui.html

用swagger2生成後端接口文檔如果覺得文檔界面不是自己喜歡的可以換一種顯示方式

如果覺得文檔界面不是自己喜歡的可以換一種顯示方式

Maven引入

            com.github.xiaoymin

            swagger-bootstrap-ui

            1.9.6

運作檢視文檔

http://localhost:8080/doc.html

用swagger2生成後端接口文檔如果覺得文檔界面不是自己喜歡的可以換一種顯示方式