R返回对象

本文介绍了如何在SpringBoot项目中创建一个返回的公共类R.java,用于处理HTTP响应,同时展示了如何配置Swagger2以生成API文档。项目包含了mybatis-plus、lombok和Thymeleaf等依赖,并在DemoController中进行了测试。
摘要由CSDN通过智能技术生成

每次做项目的时候,都需要编写一个返回的公共类,结合自己所学,我觉得下面编写的公共类最方便,特此记录一下。

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

测试的时候需要将端口号修改成自己的端口号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值