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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Narandhran Thangavel
  • 1,392
  • 1
  • 12
  • 20
  • You can use biding result in your application for this type of check. – Pawan Tiwari Apr 04 '19 at 11:19
  • Possible duplicate of [What is the best way to know if all the variables in a Class are null?](https://stackoverflow.com/questions/12362212/what-is-the-best-way-to-know-if-all-the-variables-in-a-class-are-null) – Mebin Joe Apr 04 '19 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 – Narandhran Thangavel Apr 04 '19 at 11:27
  • here it should be logic in default case but you have to check only null values, i don't know any way is there for same, but you can use regex to write such code and avoid typing. – pks Apr 04 '19 at 11:27
  • ok ill go through the regex concepts and Thanks for your guidence! @pks – Narandhran Thangavel Apr 04 '19 at 11:33
  • 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. – Sudhir Ojha Apr 04 '19 at 11:35
  • use any powerful editor i.e. editplus etc. and find and replace regex. – pks Apr 04 '19 at 11:37
  • But what if we have more than 10 attributes? code for 10 times checking the null value is that efficient way? @SudhirOjha – Narandhran Thangavel Apr 04 '19 at 11:38
  • 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_ Apr 05 '19 at 05:50

2 Answers2

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.

sinujohn
  • 2,506
  • 3
  • 21
  • 26
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
ASK
  • 1,136
  • 1
  • 10
  • 14