SpringBoot 整合 Swagger

1.引入依赖

     
            com.spring4all
            swagger-spring-boot-starter
            1.9.0.RELEASE
        

2.在 application.yml 中进行配置

swagger:
  base-path: /**
  base-package: 'com.example.demo'
  title: 'spring-boot-swagger-demo'
  description: '基于Swagger构建的SpringBoot RESTApi 文档'
  version: '1.0'
  contact:
    name: '空夜'
    url: 'http://www.eknown.cn'
    email: '[email protected]'

3.在启动类上添加注解 @EnableSwagger2

image.png

4.在 controller 层添加注解

@Api(tags = "用户管理")
@RestController
@RequestMapping("api/user")
public class UserController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    //查看所有
    @ApiOperation("查看所有用户")
    @GetMapping("findAll")
    public RestResponse findAll() {
        return success(userService.findAll());
    }


    //根据id查看
    @ApiOperation("根据用户 id 查看用户")
    @GetMapping("findById")
    public RestResponse findById(@RequestParam(name = "id") Integer id) {
        try {
            List user = userService.findById(id);
            if (user.size() == 0) {
                return notFound("The data does not exist");
            }
            return success(user);
        } catch (Exception e) {
            LOG.error("Get user error.", e);
            return error("GET USER INFORMATION FAILED!");
        }
    }

5.在实体层添加注解

@ApiModel(description = "用户类")
@Table(name = "user")
public class User {

    @Id
    @KeySql(useGeneratedKeys = true)
    private Integer id;
    @ApiModelProperty(value = "用户名")
    private String name ;
    private Integer age;
    private String email;

最后访问 :http://localhost:8888/swagger-ui.html

image.png

你可能感兴趣的:(SpringBoot 整合 Swagger)