[英]How to not return null value in ResponseEntity?
我使用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
控制器/名稱控制器.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);
}
模型/名稱Model.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
在 spring 引導視圖https://docs.spring.io/spring-boot/docs/2.4.0/reference/htmlsingle並搜索spring.jackson.default-property-inclusion
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.