引入pom相关依赖
<!--swagger2-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--访问路径:http://localhost:8080/swagger-ui.html-->
<!-- <dependency>-->
<!-- <groupId>io.springfox</groupId>-->
<!-- <artifactId>springfox-swagger-ui</artifactId>-->
<!-- <version>2.9.2</version>-->
<!-- </dependency>-->
<!--访问路径:http://localhost:8080/doc.html-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.5</version>
</dependency>
提供了两种UI界面,选择一个喜欢的即可
添加config配置文件
SwaggerConfig:
package com.luoye.csdn_course_server.config;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
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.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* @author LUOYE
* @date 2022/6/16
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Value("${swagger.enable}")
private Boolean enable;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2).enable(enable)
.pathMapping("/")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
.title("落叶服务接口文档")
.description("接口文档及相关说明")
.version("1.0.1")
.contact(new Contact("落叶","blog.csdn.net","441374668@qq.com"))
.license("The Apache License")
.licenseUrl("http://www.baidu.com")
.build())
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeyList= new ArrayList<>();
apiKeyList.add(new ApiKey("token", "token", "header"));
return apiKeyList;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts=new ArrayList<>();
securityContexts.add(
SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("^(?!auth).*$"))
.build());
return securityContexts;
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> securityReferences=new ArrayList<>();
securityReferences.add(new SecurityReference("token", authorizationScopes));
return securityReferences;
}
}
config文件中调用了一个swagger.enable,来自配置文件。作用是控制swagger页面是否可以使用,如果为false,则swagger页面将不可访问。
yml配置文件中加入:
swagger:
enable: true
使用swagger注解
Entity实体类中使用@ApiModel、@ApiModelProperty
package com.luoye.csdn_course_server.user.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.apache.ibatis.type.Alias;
import java.io.Serializable;
/**
* @author NTY
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="User对象", description="")
@Alias("User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "用户名")
@TableField("account")
private String account;
@ApiModelProperty(value = "密码")
@TableField("password")
private String password;
}
controller中使用@Api、@ApiOperation
package com.luoye.csdn_course_server.user.controller;
import com.luoye.csdn_course_server.user.req.RegisterUserParam;
import com.luoye.csdn_course_server.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author NTY
*/
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
public class UserController {
@Resource
private IUserService userService;
@PostMapping(value = "/register")
@ApiOperation(value = "用户注册接口")
public String registerAccount(@RequestBody RegisterUserParam param){
if(!StringUtils.isNotBlank(param.getAccount())||!StringUtils.isNotBlank(param.getPassword())){
return "请输入账号或密码";
}
return userService.registerUser(param);
}
}
启动项目,访问http://localhost:18019/doc.html
端口是我项目自己配置的,根据你自己的项目修改
通过调试就可以对自己编写的接口进行测试了。
注解更多的使用方法和功能,就请自行百度吧。