【发布时间】:2020-11-17 03:31:17
【问题描述】:
我使用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
控制器/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;
}
}
【问题讨论】:
-
只需在班级级别将
@JsonInclude(Include.NON_NULL)添加到NameModel -
看来您没有使用 Spring Boot,或者您正在重新声明免费提供的东西,因为默认情况下会抑制空值。不过,无论哪种情况,@DCTID 的答案都是直接的。
-
@chrylis-cautiouslyoptimistic- 我确实使用 Spring Boot。你能否澄清一下
re-declaring things that come for free?。如果可以,你愿意举个例子吗?对此,我真的非常感激。无论如何,上面的代码是一个示例代码,没有使用额外的代码,我还有一个正在进行的项目,没有定义构造函数,但显示了确切的行为`null 包含在响应中'github.com/kidfrom/g2_java/blob/main/Bank_Account/java/…
标签: java spring spring-boot mybatis