java - 如何在 ResponseEntity 中不返回空值?

标签 java spring spring-boot mybatis

我使用 ResponseEntity 为 GET“api/v1/name”和 POST“api/v1/name”请求返回响应。

我的目标是不返回空值的响应,例如在 POST "api/v1/name"请求中,当前响应正文将是:

{
    "id": null,
    "name": "who",
    "newid": "A8C90A53-78F6-4BD6-9628-CBA8FC349C08"
}

我希望它看起来像:

{
    "name": "who",
    "newid": "A8C90A53-78F6-4BD6-9628-CBA8FC349C08"
}

在我看来,使用下面这段代码重新创建对象只会降低代码的可读性并且可能会占用更多内存(我不确定,如果我错了请告诉我):

...
Map<String, String> responseBody = new HashMap<>();
responseBody.put("name", nameModel.getName());
responseBody.put("newid", nameModel.getNewId());

return new ResponseEntity<>(responseBody, HttpStatus.OK);

==== 下面是完整的存储库,如果您想查看更新的存储库:https://github.com/kidfrom/g2_java/tree/main/etc/mssqlserver

Controller /NameController.java

package com.example.mssqlserver.controller;

import com.example.mssqlserver.mapper.NameMapper;
import com.example.mssqlserver.model.NameModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class NameController {

  @Autowired
  private NameMapper nameMapper;

  @GetMapping("api/v1/name")
  public ResponseEntity<?> selectAll() {
    return new ResponseEntity<>(nameMapper.selectAll(), HttpStatus.OK);
  }

  @PostMapping("api/v1/name")
  public ResponseEntity<?> insert(@RequestBody NameModel nameModel) {

    // validator
    if (!nameModel.requestIsValid()) {
      return ResponseEntity.badRequest().build();
    }

    if (nameMapper.insert(nameModel) == 1) {
      return new ResponseEntity<>(nameModel, HttpStatus.OK);
    } else {
      return ResponseEntity.badRequest().build();
    }
  }
}

映射器/NameMapper.java

package com.example.mssqlserver.mapper;

import com.example.mssqlserver.model.NameModel;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface NameMapper {

  @Select("SELECT * FROM name")
  public List<NameModel> selectAll();

  @SelectKey(statement = "SELECT NEWID()", keyProperty = "newid", resultType = String.class, before = true)
  @Insert("INSERT INTO name (name, newid) VALUES (#{name}, #{newid})")
//  @Options(useGeneratedKeys = true, keyProperty = "id")
  int insert(NameModel nameModel);
}

模型/NameModel.java

package com.example.mssqlserver.model;

public class NameModel {
  private Integer id;
  private String name;
  private String newid;

  public NameModel(Integer id, String name, String newid) {
    this.id = id;
    this.name = name;
    this.newid = newid;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public String getNewid() {
    return newid;
  }

  public void setNewid(String newid) {
    this.newid = newid;
  }

  public boolean requestIsValid() {
    if (this.name.isEmpty()) return false;

    return true;
  }
}

最佳答案

我认为这个简单的解决方案可以帮助您

https://stackoverflow.com/a/36515285/5108695

由于正在使用 Jackson,您必须将其配置为 Jackson 属性。对于 Spring Boot REST 服务,您必须在 application.properties 或 application.yml 中配置它:

spring.jackson.default-property-inclusion = NON_NULL

关于java - 如何在 ResponseEntity 中不返回空值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64868946/

相关文章:

java - Firebase:无法验证 Firebase ID token 的签名

java - 如何禁用 Spring 记录 DEBUG 消息?

java - 列出 Vertx 中所有已注册的路由

java - 使用springboot时如何在mybatis-config.xml中设置数据源?

java - 如何配置 Spring Boot JPA Java 实体以自动修剪每个 CHAR 列中的字符串?

java - 使用 Spring-Boot 和 Jetty 设置 SSL

java - Android:打开listView Activity

java - 替换文本边界内的表达式

java - Spring Boot 安全性 - 如果缺少授权 header ,则使用 Cookie 中的 token

java - Spring Ehcache MBean 监控