1

I'm using spring boot for restful webservices, and I've many DTO and Model objects.

When doing post request, front end user is sending a DTO type object. Dto has mostly similar members of Model object. I'm checking the null constraint of each member in DTO object and if not then set the value to similar attributes of MODEL object.

I've briefly defined my case below,

class UserDto{
    private String userid;
    private String username;
    private String dept;
    public String getUserid() {
        return userid;
    }
    public void setUserid(String userid) {
        this.userid = userid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
}

And pojo

 class User {
  private String userid;
  private String username;
  private String dept;


   //some constructors
   ........

   public String getUserid() {
    return userid;
   }
   public void setUserid(String userid) {
    this.userid = userid;
   }
   public String getUsername() {
    return username;
   }
   public void setUsername(String username) {
    this.username = username;
   }
   public String getDept() {
    return dept;
   }
   public void setDept(String dept) {
    this.dept = dept;
    }
 }

I check every time like this, now I just want to know is there any consistent way to do this

if (userDto.getUserid()!= null)
       user.setUserid(userDto.getUserid());
  if (userDto.getUsername()!= null)
       user.setUsername(userDto.getUsername());

I've already looked at this link What is the best way to know if all the variables in a Class are null? . This only tells that how to find the nullable members in an object, but in my case I want to find the not null member and copy to the another member in an object. ie if userDto.getUsername()!==null then user.setUsername(userDto.getUsername()). This is all I need.

9
  • You can use biding result in your application for this type of check. Commented Apr 4, 2019 at 11:19
  • Possible duplicate of What is the best way to know if all the variables in a Class are null?
    – Mebin Joe
    Commented Apr 4, 2019 at 11:19
  • Your mentioned link just telling to find the nullable members in an object, but in my case i want to find the not null member and copy to the another member in an object. ie) if userDto.getUsername()!==null then user.setUsername(userDto.getUsername()). This is all I need. @MebinJoe Commented Apr 4, 2019 at 11:27
  • 1
    The way you are checking null value and setting in user object is correct way i mean you have more control on it like you are doing. This is something called business logic. Commented Apr 4, 2019 at 11:35
  • 2
    What you're doing here is the correct way. As @SudhirOjha pointed out it is part of your business logic. A few null comparisons are ALWAYS quicker (since you're comparing pointers) and hence more efficient, than some voodoo with reflection or mapping using a whole framework during runtime. Your code looks ugly, true, but everyone understands what's happening and that is a tradeoff that you should be happily willing to accept. It may take a bit longer to code, but if you have to change something or add a special case your fix will be easy so in the longrun you will even save time and money.
    – mind_
    Commented Apr 5, 2019 at 5:50

2 Answers 2

1

This seems to be the usecase of ModelMapper project. I haven't used it, but glancing through the docs over here: http://modelmapper.org/getting-started/#mapping it seems ModelMapper solves your exact same problem.

1

If you want a consistant way, do this. 1. Convert your dto object to string using objectMapper. 2. Map this string to your pojo.

 ObjectMapper objectMapper = new ObjectMapper();
 objectMapper.setDefaultPropertyInclusion(Include.NON_NULL);
 String userDtoString = objectMapper.writeValueAsString(userDto);

 User user = objectMapper.readValue(userDtoString, User.class);  // your desired user class object without null values

Not the answer you're looking for? Browse other questions tagged or ask your own question.