Home > Java > javaTutorial > How SpringBoot builds API documentation based on Swagger2

How SpringBoot builds API documentation based on Swagger2

PHPz
Release: 2023-05-10 23:04:04
forward
1091 people have browsed it

1. Add dependencies

<!--SpringBoot使用Swagger2构建API文档的依赖-->
    <dependency>
      <groupid>io.springfox</groupid>
      <artifactid>springfox-swagger2</artifactid>
      <version>2.7.0</version>
    </dependency>

    <dependency>
      <groupid>io.springfox</groupid>
      <artifactid>springfox-swagger-ui</artifactid>
      <version>2.7.0</version>
    </dependency>
Copy after login

2. Create Swagger2 configuration class

package com.offcn.config;

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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration//表示该类为一个配置类,相当于spring中的xml配置文件
@EnableSwagger2 //开启在线文档
public class SwaggerConfig {

  //1.声明 api 文档的属性
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Spring Boot中使用Swagger2构建RESTful APIs")
        .description("优就业")
        .termsOfServiceUrl("http://www.ujiuye.com/")
        .contact("小刘同学")
        .version("1.0")
        .build();
  }

  //配置核心配置信息
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))
        .paths(PathSelectors.any())
        .build();
  }
}
Copy after login

3. Modify the Controller and add documentation comments

Add descriptions to the API through the @ApiOperation annotation

Add descriptions to the parameters through the @ApiImplicitParams@ApiImplicitParam annotation

package com.offcn.controller;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/rest")
@RestController
public class RestFulController {

  @Autowired
  private UserDao userDao;

  @GetMapping("/getUserById")
  @ApiOperation(value="查找指定id用户信息", notes="根据id查找用户信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
  })
  public User getUserById(Integer id){
    User user = userDao.getOne(id);
    return user;
  }

  @DeleteMapping("/del")
  @ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
  })
  public String delUserById(Integer id){
    userDao.deleteById(id);
    return "success";
  }
}
Copy after login

How SpringBoot builds API documentation based on Swagger2

4. View Swagger2 documentation

Restart the project

Visit:

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

The above is the detailed content of How SpringBoot builds API documentation based on Swagger2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template