内容参考:https://www.cnblogs.com/byuan/p/14988295.html
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
package com.swagger.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger的配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径,控制器类包
.apis(RequestHandlerSelectors.basePackage("com.swagger.controller"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 集成 Swagger2 测试接口文档")
//创建人
.contact(new Contact("王晓明", "http://www.beidu.com", "[email protected]"))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
http://localhost:8080/swagger-ui.html
注:如果项目启动报错
这里使用的Swagger版本是2.9.2、springboot 版本是2.6.3
发现是springboot版本太高,缺少swagger运行所需要的环境,回退到之前的版本
把springboot回退到2.5.6即可正常启动
package com.swagger.entity;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* 实体类
*/
@NoArgsConstructor// 生成无参的构造方法
@AllArgsConstructor// 生成满参的构造方法
@Accessors(chain = true)// 使用链式调用
@Data// 自动生成get/set方法、重写toString方法等方法
public class Student implements Serializable {
@ApiModelProperty(value = "学生id")// 对属性进行简要说明
private Integer studentId;
@ApiModelProperty(value = "学生姓名")
private String studentName;
@ApiModelProperty(value = "学生分数")
private Double studentScore;
}
package com.swagger.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
* 定义一个返回结果类
*/
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
public class ResponseVo {
private String message; //操作的提示信息
private Integer status; //响应状态码
private E data; //获取数据
}
package com.swagger.service;
import com.swagger.entity.Student;
import com.swagger.vo.ResponseVo;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Service
*/
@Service
public class StudentService {
// 这里我们不使用数据库, 使用Map集合来模拟数据库中的表
private static Map studentMap=new HashMap<>();
private static Integer studentId=10001;
static {
studentMap.put(studentId, new Student(studentId, "王晓明", 98.5));
studentId++;
studentMap.put(studentId, new Student(studentId, "王晓晓", 95.5));
studentId++;
studentMap.put(studentId, new Student(studentId, "王晓华", 96.5));
studentId++;
}
/**
* 插入一名学生返回影响行数
* @param student
* @return
*/
public ResponseVo addOneStudent(Student student){
student.setStudentId(studentId);
studentMap.put(studentId, student);
studentId++;
return new ResponseVo<>("插入一条数据成功", 200, 1);
}
/**
* 删除一位学生返回影响行数
* @param studentId
* @return
*/
public ResponseVo deleteOneStudentByStudentId(Integer studentId){
if(studentMap.containsKey(studentId) == false){
return new ResponseVo<>("您输入的id不存在", 200, 0);
}
studentMap.remove(studentId);
return new ResponseVo<>("删除成功", 200, 1);
}
/**
* 修改一位学生返回影响行数
* @param student
* @return
*/
public ResponseVo updateOneStudent(Student student){
if(studentMap.containsKey(student.getStudentId()) == false){
return new ResponseVo<>("根据学生id,您所修改的学生不存在", 200, 0);
}
studentMap.put(student.getStudentId(), student);
return new ResponseVo<>("学生修改成功", 200, 1);
}
/**
* 输入studentId查询并返回对应的学生
* @param studentId
* @return
*/
public ResponseVo getOneStudentByStudentId(Integer studentId){
if(studentMap.containsKey(studentId) == false){
return new ResponseVo<>("您所查询的学生不存在", 200, null);
}
return new ResponseVo<>("查询成功", 200, studentMap.get(studentId));
}
/**
* 获取所有学生
* @return
*/
public ResponseVo> getAllStudent(){
return new ResponseVo<>("获取全部学生成功", 200, studentMap.values());
}
}
package com.swagger.controller;
import com.swagger.entity.Student;
import com.swagger.service.StudentService;
import com.swagger.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
/**
* Controller
*/
@Api("学生管理相关接口")
@RestController //@Controller + @ResponseBody
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
/**
* 添加一名学生
* @param student
* @return
*/
@ApiOperation("添加一名学生")// 为每个handler添加方法功能描述
@PostMapping("/add_student.action")
@ApiImplicitParam(name = "student", value = "所添加的学生", dataTypeClass = Student.class)
public ResponseVo addOneStudent(Student student) {
return studentService.addOneStudent(student);
}
/**
* 根据studentId删除一名学生
* @param studentId
* @return
*/
@ApiOperation("根据studentId删除一名学生")
@DeleteMapping("/delete_student/{studentId}.action")
public ResponseVo deleteOneStudentByStudentId(@PathVariable Integer studentId) {
return studentService.deleteOneStudentByStudentId(studentId);
}
/**
* 修改一名学生
* @param student
* @return
*/
@ApiOperation("修改一名学生")
@PutMapping("/update_student.action")
@ApiImplicitParams({
@ApiImplicitParam(name = "studentId", value = "学号", required = true), //required为是否必填项
@ApiImplicitParam(name = "studentName", value = "学生姓名", required = false),
@ApiImplicitParam(name = "studentSex", value = "学生性别", required = false),
@ApiImplicitParam(name = "studentScore", value = "学生分数", required = false)
})
public ResponseVo updateOneStudent(Student student) {
return studentService.updateOneStudent(student);
}
/**
* 根据id获取一名学生
* @param studentId
* @return
*/
@ApiOperation("根据id获取一名学生")
@GetMapping("/get_ont_student/{studentId}.action")
public ResponseVo getOntStudentByStudentId(@PathVariable Integer studentId) {
return studentService.getOneStudentByStudentId(studentId);
}
/**
* 获取全部学生
* @return
*/
@ApiOperation("获取全部学生")
@GetMapping("/get_all_student.action")
public ResponseVo> getAllStudent() {
return studentService.getAllStudent();
}
}
源码地址:https://gitee.com/wang-jian-fei/springboot-swagger-mongoDB.git
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦