3

I use ResponseEntity to return response for a GET "api/v1/name" and POST "api/v1/name" request.

My goal is not to return response with null value, for example in a POST "api/v1/name" request, currently the response body would be:

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

What I wish it would look like:

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

In my opinion, recreating the object using this code below would only make the code less readable and maybe use more memory (I am not sure, please let me know if I am wrong):

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

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

==== Down below is the full repository, if you wish to see the updated one: 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();
    }
  }
}

mapper/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/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;
  }
}
3
  • 4
    Just add @JsonInclude(Include.NON_NULL) to NameModel at the class level Commented Nov 17, 2020 at 3:55
  • It appears that either you aren't using Spring Boot, or you're re-declaring things that come for free, because nulls would be suppressed by default. In either case, though, @DCTID's answer is the direct one. Commented Nov 17, 2020 at 3:55
  • @chrylis-cautiouslyoptimistic- I do use Spring Boot. Could you please clarify about re-declaring things that come for free?. If you may, would you like to give the example? I really appreciate it. Anyway, the code above is a sample code, no extra code is used, I also have ongoing project without a constructor defined yet show exact behavior `null is included in the response' github.com/kidfrom/g2_java/blob/main/Bank_Account/java/… Commented Nov 17, 2020 at 14:35

2 Answers 2

8

I think this simple solution could help you

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

Since Jackson is being used, you have to configure that as a Jackson property. In the case of Spring Boot REST services, you have to configure it in application.properties or application.yml:

spring.jackson.default-property-inclusion = NON_NULL
Sign up to request clarification or add additional context in comments.

Comments

0

In spring boot view https://docs.spring.io/spring-boot/docs/2.4.0/reference/htmlsingle and search spring.jackson.default-property-inclusion

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.