springboot教程(二) - 集成bootstrap-swagger

springboot集成swagger发布接口

pom文件加入相关依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>

创建实体类

package ichpeng.springboot.demo.bean;

import lombok.Data;

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 17:00
 * @description:
 */
@Data
public class Student {
    String studentId;
    String name;
    String clazz;
}

package ichpeng.springboot.demo.bean;

import lombok.Data;

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 16:58
 * @description:
 */
@Data
public class Teacher {
    String teacherId;
    String name;
    String subject;
    String[] classes;
}


创建相关接口(Controller)

package ichpeng.springboot.demo.controller;

import ichpeng.springboot.demo.bean.Student;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 17:02
 * @description:
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @GetMapping("/newStudent")
    public Student newStudent(String id, String name, String clazz){
        Student student = new Student();
        student.setStudentId(id);
        student.setName(name);
        student.setClazz(clazz);
        return student;
    }
}

package ichpeng.springboot.demo.controller;

import ichpeng.springboot.demo.bean.Teacher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 17:02
 * @description:
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {

    @GetMapping("/getById")
    public Teacher getById(String id){
        Teacher teacher = new Teacher();
        teacher.setTeacherId(id);
        teacher.setName("teacher-name_" + id);
        teacher.setSubject("english");
        teacher.setClasses(new String[]{"2021级18班", "2021级19班"});
        return teacher;
    }
}

新增swagger配置类

class要加@Configuration及@EnableSwagger2注解

package ichpeng.springboot.demo.config;

import org.springframework.beans.factory.annotation.Value;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author: ichpeng@qq.com
 * @date: 2021/3/16 17:06
 * @description:
 */
@Configuration //必须存在
@EnableSwagger2 //必须存在
public class SwaggerConfig {
    @Value("${application.version}")
    private String appVersion;

    @Bean("custom-docket")
    public Docket customDocket(){
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
    }
    @Bean("demo-docket")
    public Docket demoDocket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("师生信息")
                .select()
                .apis(RequestHandlerSelectors.basePackage("ichpeng.springboot.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("ichpeng", "https://blog.csdn.net/ichpeng", "ichpeng@qq.com");
        return new ApiInfoBuilder()
                .title("师生信息管理")
                .description("师生信息管理接口")
                .contact(contact)
                .version(appVersion)
                .build();
    }
}

测试

启动程序,访问http://127.0.0.1:8000/doc.html
在这里插入图片描述
测试接口
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值