问题描述

针对界面的多条件复杂查询,由于查询条件不定,不好一个条件定义一个参数。创建一个查询类来接收参数。

在查询为Get模式时,SpringDoc 的接口调试参数输入界面异常,且后端无法接收查询参数。

环境

  <!-- mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>

  <!-- knife4j openapi3 接口文档 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>

查询类 UserQuery

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;


@Data
@ToString(callSuper = true)
@Schema(description = "用户查询条件实体")
public class UserQuery {

    @Schema(description = "用户名关键字",example = "o")
    private String name;

    @Schema(description = "用户状态:1-启用,0-停用", example = "1")
    private String status;

    @Schema(description = "余额最小值", example = "2000")
    private Integer minBalance;

    @Schema(description = "余额最大值", example = "20000")
    private Integer maxBalance;
}

Controller 中的查询方法

    @Operation(
            summary = "06 根据复杂条件查询用户接口"
    )
    @GetMapping("/list")
    public List<UserVO> queryUsers( @RequestBody UserQuery query){
        //1.查询用户
        List<User> users = userService.queryUsers(
                query.getName(),query.getStatus(),query.getMaxBalance(),query.getMinBalance()
        );

        //2.把PO 拷贝 VO
        return BeanUtil.copyToList(users, UserVO.class);
    }

UserVO类

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.ToString;

import java.time.LocalDateTime;

@Data
@ToString(callSuper = true)
@Schema(description = "用户 VO 实体")
public class UserVO {

    @Schema(description = "用户id")
    private Long id;

    @Schema(description = "用户名")
    private String userName;

    @Schema(description = "详细信息")
    private String info;

    @Schema(description = "用户状态(1启用 0停用)")
    private String status;

    @Schema(description = "账户余额")
    private Integer balance;

    @Schema(description = "注册时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime regDate;

    @Schema(description = "创建时间")//, type = "dateTime",pattern = "yyyy-MM-dd HH:mm:ss"
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime createTime;

    @Schema(description = "最后更新时间")//, type = "dateTime",pattern = "yyyy-MM-dd HH:mm:ss"
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime updateTime;
}

PO: User 类

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.sunmon.turtle.utils.MyLocalDateTypeHandler;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;


@Data
@Component
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
//    @TableId//不指定 IdType 时,默认使用雪花算法生成数字(企业级开发时使用)
    @TableId(type= IdType.AUTO)//自增
    private Long id;
    private String userName;
    private String password;
    private String phone;
    private String gender;
    private String info;
    private String loginId;
    private Integer status;//用户状态(1启用 0停用)
    @TableField(exist = false)
    private String address;
    private Integer balance;

    @TableField( typeHandler = MyLocalDateTypeHandler.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime regDate;

    @TableField( typeHandler = MyLocalDateTypeHandler.class, fill = FieldFill.INSERT)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    @TableField( typeHandler = MyLocalDateTypeHandler.class, fill = FieldFill.UPDATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;


}

结果(有问题的)

尝试发送请求,点发送

后端问题提示

问题解决

查百度

调整请求方式

重启服务器,再看,请求参数已变

尝试发请求,点“发送”

已能正常获取结果

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐