Home » Java » Spring boot DTO is null at Controller

Spring boot DTO is null at Controller

Posted by: admin October 22, 2018 Leave a comment

Questions:

Am new to Spring boot. Am trying to create an endpoint that will add vehicle under manager. I have two entites Vehicle and Manager and corresponding DTO are below. ManagerDTO has reference of VehicleDTO

VehicleDTO

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.EngineType;
import com.Manufacturer;

@JsonDeserialize(builder = VehicleDTO.VehicleDTOBuilder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class VehicleDTO {

    private Long vehicleid;

    @NotNull(message = "vehcilenumber can not be null!")
    @Size(min = 2, max = 14)
    private String vehcilenumber;

    @NotNull(message = "Engine Type can not be null!")
    private EngineType enginetype;

    @Min(1)
    @Max(5)
    private Integer rating;

    private Manufacturer manufacturer;

    private VehicleDTO(Long id, String vehcilenumber, EngineType enginetype,Integer rating,Manufacturer manufacturer){
        this.vehcilenumber=vehcilenumber;
        this.enginetype=enginetype;
        this.rating=rating;
        this.vehicleid=id;
        this.manufacturer=manufacturer;
    }

    public static VehicleDTOBuilder newBuilder()
    {
        return new VehicleDTOBuilder();
    }

    public Long getvehicleid() {
        return vehicleid;
    }


    public String getvehcilenumber() {
        return vehcilenumber;
    }

    public EngineType getEnginetype() {
        return enginetype;
    }



    public Integer getRating() {
        return rating;
    }


    public Manufacturer getManufacture() {
        return manufacturer;
    }



    @JsonPOJOBuilder(buildMethodName = "createVehicleDTO", withPrefix = "set")
    public static class VehicleDTOBuilder{

        private Long vehicleid;
        private String vehcilenumber;
        private EngineType enginetype;
        private Integer rating;
        private Manufacturer manufacturer;

        public VehicleDTOBuilder setvehicleid(Long id) {
            this.vehicleid = id;
            return this;
        }
        public VehicleDTOBuilder setvehcilenumber(String vehcilenumber) {
            this.vehcilenumber = vehcilenumber;
            return this;
        }

        public VehicleDTOBuilder setEnginetype(EngineType enginetype) {
            this.enginetype = enginetype;
            return this;
        }
        public VehicleDTOBuilder setRating(Integer rating) {
            this.rating = rating;
            return this;
        }


        public VehicleDTOBuilder setManufacturer(Manufacturer manufacturer) {
            this.manufacturer = manufacturer;
            return this;
        }

        public VehicleDTO createVehicleDTO()
        {
            return new VehicleDTO(vehicleid, vehcilenumber, enginetype,rating,manufacturer);
        }

    }

}

And my ManagerDTO is below. This has reference of Vehcile DTO

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.VehicleDO;
import javax.validation.constraints.NotNull;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class ManagerDTO {
    @JsonIgnore
    private Long id;

    @NotNull(message = "Username can not be null!")
    private String username;

    @NotNull(message = "Password can not be null!")
    private String password;

    private VehicleDO vehicle;

    private ManagerDTO() {
    }

    private ManagerDTO(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    private ManagerDTO(Long id, String username, String password,VehicleDO vehicle) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.vehicle = vehicle;
    }

    public static ManagerDTOBuilder newBuilder() {
        return new ManagerDTOBuilder();
    }

    @JsonProperty
    public Long getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public VehicleDO getvehicle() {
        return vehicle;
    }

    public static class ManagerDTOBuilder {
        private Long id;
        private String username;
        private String password;
        private VehicleDO vehicle;

        public ManagerDTOBuilder setId(Long id) {
            this.id = id;
            return this;
        }

        public ManagerDTOBuilder setvehicle(VehicleDO vehicle) {
            this.vehicle = vehicle;
            return this;
        }

        public ManagerDTOBuilder setUsername(String username) {
            this.username = username;
            return this;
        }

        public ManagerDTOBuilder setPassword(String password) {
            this.password = password;
            return this;
        }


        public ManagerDTO createManagerDTO() {
            return new ManagerDTO(id, username, password,vehicle);
        }

    }
}

And my controller is below

@PostMapping("/add")
    public void mapVehicleAndManager(@Valid @RequestBody ManagerDTO managerDTO)
    {
        System.out.println("Print: "+managerDTO.getvehicle().getvehicleId());//this print null
    }

So when my controller is called i can see username and password are populated where as vehicle is not populated. A not sure what am missing here and reason why jackson is not populating my java object.

Answers: