springboot整合swagger简单实例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_39150374/article/details/100280220

前言

springboot版本1.5.x,前后端分离,一般我们写的接口都可以用postman来测试,swagger也可以用来测试写好的,所以就整合一下swagger。

pom文件

  <!--Swagger的配置依赖包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>

swagger配置文件

package com.test.testdemo.config;

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;

/**
 * @Desc swagger配置文件
 */
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.testdemo.controller")) //基础扫描包路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("配置swagger")
                .description("配置swagger")
                .termsOfServiceUrl("http://localhost:9004") //地址
                .contact("ceshi") //负责人
                .version("0.1")
                .build();
    }
}

controller代码

package com.test.testdemo.controller;

import com.test.testdemo.entity.secondary.Student;
import com.test.testdemo.response.ResEntity;
import com.test.testdemo.servcie.StudentServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @Desc 学生管理
 */
@Api(value = "学生管理")
@RestController
@RequestMapping("api/student")
public class StudentController {
    @Autowired
    StudentServiceImpl studentService;

    /**
     * 新增学生信息
     *
     * @param student
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增学生信息", notes = "新增学生信息", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object create(@RequestBody Student student) {
        return ResEntity.success(studentService.create(student));
    }

    /**
     * 查找一条学生信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查找一条学生信息", notes = "查找一条学生信息", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object findOne(@PathVariable Integer id) {
        return ResEntity.success(studentService.findOne(id));
    }
}

测试结果

测试地址: http://localhost:9004/swagger-ui.html#/ (自己项目启动的地址)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39150374/article/details/100280220