Springboot学习笔记1之Springboot整合Swagger2

创建Springboot简单项目

创建简单Springboot项目,按照指引一步步完成创建

导入依赖jar包

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

注意:此处引入的jar包和Springboot版本的兼容性,此处有坑。。。

编写Swagger2的配置类


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
class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.yusys.springboot01"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("Springboot整合Swagger2测试")
                .description("Springboot创建RestfulAPI测试")
                .termsOfServiceUrl("")
                .contact("程序")
                .version("1.0")
                .build();
    }
}

@Configuration:告诉Springboot该类为配置类。
@EnableSwagger2:告诉Springboot容器启用Swagger2功能。

创建Controller处理类


import com.yusys.springboot01.entity.Customer;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value="/customers")
public class CustomerHandler {
    //创建安全的线程
    static Map<String, Customer>  customerMap= Collections.synchronizedMap(new HashMap<String,Customer>());

    /**
     * 查询Customer列表
     * @return
     */
    @ApiOperation(value="查询Customer列表",notes="")
    @RequestMapping(value="/",method = RequestMethod.GET)
    public List<Customer> getCustomerList(){
        System.out.println("开始执行getCustomerList()方法------");
        //处理 /customers的请求,获取customer列表
        List<Customer> list=new ArrayList<Customer>(customerMap.values());
        return list;
    }

    /**
     * 新增customer
     * @param customer
     * @return
     */
    @ApiOperation(value="添加Customer",notes="根据customer信息添加Customer")
    @ApiImplicitParam(name="customer",value = "customer详情",required = true,dataType = "Customer")
    @RequestMapping(value="/",method = RequestMethod.POST)
    public String addCustomer(@ModelAttribute Customer customer){
        System.out.println("开始执行addCustomer(Customer customer)方法------");
        customerMap.put(customer.getId(),customer);
        return "success";
    }

    /**success
     * 根据ID查询Customer
     * @param id
     * @return
     */
    @ApiOperation(value="根据ID查询Customer",notes="根据ID查询Customer")
    @ApiImplicitParam(name="id",value = "客户Id",required = true,dataType = "String",paramType = "path")
    @RequestMapping(value="/{id}",method = RequestMethod.GET)
    public Customer getCustomerById(@PathVariable String id){
        System.out.println("开始执行getCustomerById(String id)方法------"+id);
        return customerMap.get(id);
    }

    /**
     *修改Customer信息
     * @param id
     * @param customer
     * @return
     */
    @ApiOperation(value="根据ID修改Customer信息",notes="根据ID修改Customer信息")
    @ApiImplicitParams({
             @ApiImplicitParam(name="id",value = "客户Id",required = true,dataType = "String",paramType = "path"),
             @ApiImplicitParam(name="customer",value = "客户详细信息",required = true,dataType = "Customer")
            })
    @RequestMapping(value="/{id}",method = RequestMethod.PUT)
    public String modifyCustomerById(@PathVariable String id,@ModelAttribute Customer customer){
        System.out.println("开始执行modifyCustomerById()方法------");
        Customer customer1=customerMap.get(id);
        customer1.setName(customer.getName());
        customer1.setAge(customer.getAge());
        customerMap.put(id,customer1);
        return  "success";
    }

    /**
     * 根据ID删除用户信息
     * @param id
     * @return
     */
    @ApiOperation(value="根据ID修改Customer信息",notes="根据ID删除Customer信息")
    @ApiImplicitParam(name="id",value = "客户Id",required = true,dataType = "String",paramType = "path")
    @RequestMapping(value="/{id}",method = RequestMethod.DELETE)
    public String deleteCustomerById(@PathVariable String id){
        System.out.println("开始执行deleteCustomerById()方法------");
        customerMap.remove(id);
        return "success";
    }

注意:@ApiImplicitParam(name=“id”,value = “客户Id”,required = true,dataType = “String”,paramType = “path”)
@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
@PathVariable String id 的组合使用
此处 paramType = “path” 不添加的话参数传递可能会有问题。

创建测试用实体类

import org.springframework.stereotype.Component;
@Component
public class Customer {
    private String id;
    private String name;
    private Integer age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

启动项目,确保工程无报错信息启动工程

** 注意:工程一定不能报错,不然无法进行后续测试 **

在浏览器地址栏输入地址查看生成的RestfulAPI可视化界面:

http://localhost:8080/swagger-ui.html
swagger_UI页面

测试添加客户信息接口

在这里插入图片描述

测试查看客户信息类表,验证添加接口的正确性

在这里插入图片描述

未完待续…

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值