1

I've the following domain and needs to return selected field in response to client. How can I achieve that using Spring?

public class Vehicle {

    private String vehicleId;
    private Long dateCreated;
    private String ownerId;
    private String colourCode;
    private String engineNumber;
    private String transmission;

    //getters & setters

    }

My objective is to return only colourCode and transmission fields to client request. I've read about DTO and seems like I can achieve my objective with DTO but I don't find any good example how to implement it. Is DTO is the correct way to achieve my objective ?

shkhssn
  • 303
  • 1
  • 4
  • 17

4 Answers4

2

Basically you just create VehicleDTO class with parameters you need

public class VehicleDTO {
    private String colourCode;
    private String transmission;
    //getters and setters
}

and then in your code you construct VehicleDTO from your Vehicle class. Fortunately, we have BeansUtils class from Spring, that uses reflection to copy properties of one object to another, because you do not want to repeat logic for copying properties for every object. So it would be something like:

  BeanUtils.copyProperties(v1, dto);

At the end your return VehicleDTO in your response instead of Vehicle

Abzal Kalimbetov
  • 505
  • 7
  • 20
  • hi @Abzal, it's work! my real situation is I need to do the DTO from Page to Page. do you have any idea how can achieve this? I found similar scenario here http://stackoverflow.com/questions/32995179/convert-type-of-spring-data-jpa-page-content but don't really know how to write convertToContactDto method. Is it means I need to place BeanUtils.copyProperties(v1, dto) inside the convertToContactDto method? – shkhssn Dec 21 '16 at 07:16
  • 1
    hi mate, I found the way to transfer Page : `final Page< VehicleDTO> vehicleDTO = post.map(VehicleConvert::convertToVehicleDTO)`. Created a new class VehicleConvert with method convertToVehicleDTO() – shkhssn Dec 21 '16 at 08:28
0

You can return IVehicle interface which exposes your properties of choice

public interface IVehicle {
   String getTransmission();
   String getColourCode();
}

and your Vehicle implents it

public class Vehicle implements IVehicle{ } 
iamiddy
  • 3,015
  • 3
  • 30
  • 33
0

There are various ways you can achieve what you want.

You can add relevant usecase / APi specific DTO for the resource.

e.g. If your API return the vehical general details you may want to expose some level of details,

public class VehicleDetailsDTO {
    private String colourCode;
    private String transmission;
    private String engineNumber; //more
    //getters and setters
}

You can then either use BeanUtils or Dozzer to convert your Vehical resource to transportable object like your DTO.

BeanUtils : http://commons.apache.org/proper/commons-beanutils/

Dozzer : http://dozer.sourceforge.net/documentation/mappings.html

ScanQR
  • 3,740
  • 1
  • 13
  • 30
0

Assuming you use JSON as output format and Jackson as serialization engine (default in Spring MVC), you can tell Jackson to not serialize null properties. Now you just need to populate the properties you need and can return the original business object.

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588