• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

SpringBoot——整合Swagger2构建Restful风格的文档

武飞扬头像
zh_Tnis
帮助2

1.概述

(1).SpringBoot创建RESTFUL风格接口,也称为表述性状态转移。如:GET、POST、PUT、DELETE等

(2).使用swagger2构建restful接口测试。

优点:

  • 可以生成文档形式的api并提供给不同团队
  • 便于自测,也便于领导查询任务量
  • 无需过度冗余的word文档

2.pom.xml

web场景 数据库连接 连接池 swagger2

  1.  
    <!-- web场景 -->
  2.  
    <dependency>
  3.  
    <groupId>org.springframework.boot</groupId>
  4.  
    <artifactId>spring-boot-starter-web</artifactId>
  5.  
    </dependency>
  6.  
    <!--导入Lombok的依赖-->
  7.  
    <dependency>
  8.  
    <groupId>org.projectlombok</groupId>
  9.  
    <artifactId>lombok</artifactId>
  10.  
    </dependency>
  11.  
    <!-- 数据库连接 -->
  12.  
    <dependency>
  13.  
    <groupId>mysql</groupId>
  14.  
    <artifactId>mysql-connector-java</artifactId>
  15.  
    <version>5.1.25</version>
  16.  
    </dependency>
  17.  
    <!--导入SpringBoot整合Mybatis相关依赖-->
  18.  
    <dependency>
  19.  
    <groupId>org.mybatis.spring.boot</groupId>
  20.  
    <artifactId>mybatis-spring-boot-starter</artifactId>
  21.  
    <version>2.0.0</version>
  22.  
    </dependency>
  23.  
    <!-- druid数据库连接池 -->
  24.  
    <dependency>
  25.  
    <groupId>com.alibaba</groupId>
  26.  
    <artifactId>druid</artifactId>
  27.  
    <version>1.1.10</version>
  28.  
    </dependency>
  29.  
    <!--增加Swagger的相关依赖-->
  30.  
    <dependency>
  31.  
    <groupId>io.springfox</groupId>
  32.  
    <artifactId>springfox-swagger2</artifactId>
  33.  
    <version>2.8.0</version>
  34.  
    </dependency>
  35.  
    <dependency>
  36.  
    <groupId>io.springfox</groupId>
  37.  
    <artifactId>springfox-swagger-ui</artifactId>
  38.  
    <version>2.8.0</version>
  39.  
    </dependency>
学新通

3.application.yml

  1.  
    spring:
  2.  
    datasource:
  3.  
    url: jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=GMT+8&&useUnicode=true&&characterEncoding=UTF-8
  4.  
    type: com.alibaba.druid.pool.DruidDataSource
  5.  
    username: root
  6.  
    password: 113846
  7.  
    driver-class-name: com.mysql.jdbc.Driver

4.bean

  1.  
    @Data
  2.  
    @AllArgsConstructor
  3.  
    @NoArgsConstructor
  4.  
    public class User {
  5.  
    private Long id;
  6.  
    private String username;
  7.  
    private Integer age;
  8.  
    }

@Data:生成get、set方法

@AllArgsConstructor:容器启动生成有参构造方法

@NoArgsConstructor:容器启动生成无参构造方法

5.mapper

  1.  
    @Mapper
  2.  
    public interface UserDao {
  3.  
    @Insert("insert into user(username,age)values(#{username},#{age})")
  4.  
    public void insert(User user);
  5.  
     
  6.  
    @Update("update user set username=#{username},age=#{age} where id=#{id}")
  7.  
    public void updateUser(User user);
  8.  
     
  9.  
    @Delete("delete from user where id=#{id}")
  10.  
    public void delete(Long id);
  11.  
     
  12.  
    @Select("select * from user")
  13.  
    public List<User> findAll();
  14.  
     
  15.  
    @Select("select * from user where id=#{id}")
  16.  
    public User findUserById(Long id);
  17.  
    }
学新通

Mapper和Repository的区别:

配置文件配置sql,Repository需要在在启动类中配置mapperscan扫描地址然后才能扫描生成bean注入到service,Mapper只要在xml配置文件中存在namespace的接口访问就能生成bean注入到service。

6.service和serviceImpl

  1.  
    @Service
  2.  
    public interface UserService {
  3.  
    public List<User> getuserList();
  4.  
     
  5.  
    public void createUser(User user);
  6.  
     
  7.  
    public User getUserById(Long id);
  8.  
     
  9.  
    public void updateUser(Long id, User user);
  10.  
     
  11.  
    public void deleteUserById(Long id);
  12.  
    }
  13.  
     
  14.  
    @Service
  15.  
    public class UserServiceImpl implements UserService {
  16.  
     
  17.  
    @Autowired
  18.  
    private UserDao userDao;
  19.  
     
  20.  
    @Override
  21.  
    public List<User> getuserList() {
  22.  
    return userDao.findAll();
  23.  
    }
  24.  
     
  25.  
    @Override
  26.  
    public void createUser(User user) {
  27.  
    userDao.insert(user);
  28.  
    }
  29.  
     
  30.  
    @Override
  31.  
    public User getUserById(Long id) {
  32.  
    return userDao.findUserById(id);
  33.  
    }
  34.  
     
  35.  
    @Override
  36.  
    public void updateUser(Long id, User user) {
  37.  
    user.setId(id);
  38.  
    userDao.updateUser(user);
  39.  
    }
  40.  
     
  41.  
    @Override
  42.  
    public void deleteUserById(Long id) {
  43.  
    userDao.delete(id);
  44.  
    }
学新通

@Service("userService"):控制反转,依赖注入。spring去创建实例

7.controller

  1.  
    @RestController //点进去看带有@Controller和@ResponseBody,自带有,下边方法就不用导了
  2.  
    @RequestMapping("/user")
  3.  
    public class UserController {
  4.  
     
  5.  
    List<User> listUser = new ArrayList<User>();
  6.  
     
  7.  
    //查询所有用户
  8.  
    @GetMapping("/")
  9.  
    //给方法增加说明,notes:备注
  10.  
    @ApiOperation(value = "查询所有的用户信息", notes = "查询用户列表")
  11.  
    public List<User> getUserList() {
  12.  
    return listUser;
  13.  
    }
  14.  
     
  15.  
    //增添用户
  16.  
    @PostMapping("/")
  17.  
    @ApiOperation(value = "新增用户", notes = "保存用户")
  18.  
    //给方法的参数增加注解,name:参数名称,dataType参数类型,value:参数功能
  19.  
    @ApiImplicitParams({
  20.  
    @ApiImplicitParam(name = "user", dataType = "User", value = "要保存的用户对象")
  21.  
    })
  22.  
    public String createUser(User user) {
  23.  
    listUser.add(user);
  24.  
    return "success";
  25.  
    }
  26.  
     
  27.  
     
  28.  
    //根据id查询用户
  29.  
     
  30.  
    @GetMapping("/{id}")
  31.  
    @ApiOperation(value = "查询指定id用户的信息", notes = "单个查询")
  32.  
    @ApiImplicitParams({
  33.  
    @ApiImplicitParam(name = "id", value = "用户的编号")
  34.  
    })
  35.  
    public User getUserById(@PathVariable("id") Long id) {
  36.  
    for (User user : listUser) {
  37.  
    if (user.getId() == id) {
  38.  
    return user;
  39.  
    }
  40.  
    }
  41.  
     
  42.  
    return null;
  43.  
    }
  44.  
     
  45.  
    //更新用户,根据id
  46.  
    @PutMapping("/{id}")
  47.  
    @ApiOperation(value = "更新指定id的用户信息", notes = "修改指定用户的相关信息")
  48.  
    @ApiImplicitParams({
  49.  
    @ApiImplicitParam(name = "id", value = "用户的Id", required = true, dataType = "Long"),
  50.  
    @ApiImplicitParam(name = "user", value = "用户的详细信息对象", required = true, dataType = "User")
  51.  
    })
  52.  
    public String updateUser(@PathVariable("id") Long id, User user) {
  53.  
    for (User user1 : listUser) {
  54.  
    if (user1.getId() == id) {
  55.  
    user1.setUsername(user.getUsername());
  56.  
    user1.setAge(user.getAge());
  57.  
    }
  58.  
    }
  59.  
     
  60.  
    return "success";
  61.  
    }
  62.  
     
  63.  
    //根据id删除用户
  64.  
    @ApiOperation(value = "删除指定id的用户信息", notes = "删除指定用户")
  65.  
    @ApiImplicitParams({
  66.  
    @ApiImplicitParam(name = "id", value = "用户的Id", required = true, dataType = "Long")
  67.  
    })
  68.  
    @DeleteMapping("/{id}")
  69.  
    public String deleteUser(@PathVariable("id") Long id) {
  70.  
    listUser.remove(getUserById(id));
  71.  
    return "success";
  72.  
    }
  73.  
    }
学新通

@RestController:复合注解,封装@Controller和@ResponseBody

@RequestMapping:配置请求路径

@ApiOperation:配置方法该restful接口请求的功能提示,notes备注方法功能页面展示

@ApiImplicitParams:配置方法请求参数

8.config

  1.  
    //指定需要生成API文档的类所在的包路径
  2.  
    //@ComponentScan(basePackages = {"路径"})
  3.  
    //声明一个配置类
  4.  
    @Configuration
  5.  
    //swagger2启动注解
  6.  
    @EnableSwagger2
  7.  
    public class SwaggerConfig {
  8.  
     
  9.  
    //生成文档相关信息
  10.  
    //标题 描述 维护者 联系人 版本 构建
  11.  
    public ApiInfo apiInfo() {
  12.  
    return new ApiInfoBuilder().title("学习SpringBoot整合Swagger2构建Restful风格的文档").description("初次使用").termsOfServiceUrl("维护者").contact("张敏").version("1.0").build();
  13.  
    }
  14.  
     
  15.  
    @Bean
  16.  
    public Docket createRestApiInfo() {
  17.  
    //文档类型为SWAGGER_2 生成这个文档的基本api信息(拿上边定义的信息构建api文档) 选中这个文档 将来给那个包下的控制器生成文档(给controller下的所有类生成restful风格的文档) 给这个路径的所有都生成 构建
  18.  
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.openlab.controller")).paths(PathSelectors.any()).build();
  19.  
    }
  20.  
    }
学新通

9.展示结果

路径:http://127.0.0.1:8080/swagger-ui.html#/

9.1页面结果学新通

9.2RESTFUL风格结果

学新通

9.2.1方法结果

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhahabh
系列文章
更多 icon
同类精品
更多 icon
继续加载