每次做项目的时候,都需要编写一个返回的公共类,结合自己所学,我觉得下面编写的公共类最方便,特此记录一下。
1、首先创建一个spring boot项目
1、在pom.xml中导入jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--swagger ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<!--mybatis-plus的springboot支持-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- thymeleaf模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、实体类R.java
package com.example.demo.pojo;
import lombok.Data;
import java.util.HashMap;
/**
* @author 东东
* @date 2023-04-02 0002
*/
@Data
public class R{
private Integer code;
private String msg;
private HashMap<String,Object> data = new HashMap<>();
public R() {}
public R(Integer code) {
this.code = code;
}
public R(Integer code, String msg) {
this.code = code;
this.msg = msg;
}
public R msg(String msg){
this.msg = msg;
return this;
}
public R code(Integer code){
this.code = code;
return this;
}
public R put(String key,Object value){
this.data.put(key, value);
return this;
}
public R put(HashMap<String,Object> data){
this.setData(data);
return this;
}
public static R ok(Integer code){
return new R(code);
}
public static R error(Integer code,String msg){
return new R(code,msg);
}
}
3、SwaggerConfig配置类
package com.example.demo.config;
import io.swagger.annotations.ApiOperation;
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;
/**
* @author 东东
* @date 2023-04-02 0002
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket coreApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(adminApiInfo())
.groupName("adminApi")
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build();
}
public ApiInfo adminApiInfo(){
return new ApiInfoBuilder()
.title("api文档")
.description("辅助api文档")
.version("1.0")
.build();
}
}
4、在controller类中进行测试
package com.example.demo.controller;
import com.example.demo.pojo.R;
import com.example.demo.pojo.R2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author 东东
* @date 2023-04-02 0002
*/
@RestController
@Api(tags = "测试信息")
public class DemoController {
@GetMapping("/test")
@ApiOperation("一条测试信息")
public R test(){
return R.ok(200,"kkk").put("data","fffdafdf").put("list","file");
}
}
5、application.yml的编写
server:
port: 8001
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/respiratory?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: root
mvc:
pathmatch:
matching-strategy: ant_path_matcher
web:
resources:
static-locations: classpath:/templates/,classpath:/static/
thymeleaf:
cache: false
mybatis-plus:
mapper-locations: classpath:/mapper/*.xml
logging:
level:
com.respiratory: debug
6、创建完成后,使用swagger进行测试
http://localhost:8001/swagger-ui.html
测试的时候需要将端口号修改成自己的端口号。