三、Springboot集成Swagger

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。
作用:

  1. 接口的文档在线自动生成。
  2. 功能测试。

1.配置pom.xml

  <!-- swagger -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.5.0</version>
    </dependency>
    <!-- swagger-ui -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.5.0</version>
    </dependency>

2.增加swagger配置类

package com.pingan.test.swagger;

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

@Configuration
@EnableSwagger2
public class SwaggerApp {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.pingan.test.controller"))
                .paths(PathSelectors.any())
                .build();
//        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 使用 Swagger2 构建RESTful API")
                //创建人
                .contact(new Contact("shuaigeyuan", "", ""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

3.配置Controller以及添加getListByPassword接口

package com.pingan.test.controller;

import com.pingan.test.entity.ItemEntity;
import com.pingan.test.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(description = "用户操作接口")
@RestController
@RequestMapping("/test")
public class TestController {

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }

    @Autowired
    private TestService testService;

    @ApiOperation("获取数据库内容")
    @RequestMapping(value="/getList",method= RequestMethod.GET)
    public List<ItemEntity> getList(){
        return testService.getList();
    }

    @ApiOperation(value = "获取用户列表", notes="通过密码获取对应的用户")
    @ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value="/getListByPassword",method= RequestMethod.GET)
    public List<ItemEntity> getListByPassword(@RequestParam(name = "password") String password){
        return testService.getListByPassword(password);
    }
}



public interface TestService {

    public List<ItemEntity> getList();

    public List<ItemEntity> getListByPassword(String password);
}


@Service("testService")
public class TestServiceImpl implements TestService {

    @Autowired
    private TestMapper testMapper;

    @Override
    public List<ItemEntity> getList() {
        return testMapper.getList();
    }

    @Override
    public List<ItemEntity> getListByPassword(String password) {
        return testMapper.getListByPassword(password);
    }
}



@Repository("testMapper")
public interface TestMapper {

    public List<ItemEntity> getList();

    public List<ItemEntity> getListByPassword(@Param("password") String password);
}




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pingan.test.mapper.TestMapper">

    <resultMap id="BaseResultMap" type="com.pingan.test.entity.ItemEntity" >
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="username" property="username" jdbcType="VARCHAR" />
        <result column="password" property="password" jdbcType="VARCHAR" />
        <result column="name" property="name" jdbcType="VARCHAR" />
    </resultMap>

    <select id="getList" resultMap="BaseResultMap">
        select id, username, password,name
        from user
    </select>

    <select id="getListByPassword" resultMap="BaseResultMap">
        select id, username, password,name
        from user
        where password=#{password}
    </select>
</mapper>
4.访问 http://localhost:8080/swagger-ui.html,可以看到如下效果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值