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
测试接口