简体   繁体   中英

ModelMapper and DozerBeanMapper don't work | Spring Boot REST API

I'm developing Spring Boot Rest Api. I have class called "Wypozyczenie", and class called "WypozyczenieDTO". I want to extract some info from class Wypozyczenie (avoid nested objects) and return data based on info from WypozyczenieDTO. I've tried DozerBeanMapper and ModelMapper and both don't convert my object properly. All the values are null or zero! I was debugging and at the line that should do my conversion it doesn't work. Here's the function in which the conversion takes place:

private WypozyczenieDto convertToDto(Wypozyczenie wypozyczenie) {
        WypozyczenieDto wypozyczenieDto = mDozerBeanMapper.map(wypozyczenie, WypozyczenieDto.class);
        return wypozyczenieDto;
    }

I was debugging and all the parameters in wypozyczenieDto are null or zeros.

Here are the Wypozyczenie and WypozyczenieDto classes (without getters and setters):

public class WypozyczenieDto {

    private Long id;

    @JsonProperty("planowana_data_rozpoczecia")
    private LocalDateTime planowanaDataRozpoczecia;

    @JsonProperty("planowana_data_zakonczenia")
    private LocalDateTime planowanaDataZakonczenia;

    @JsonProperty("faktyczna_data_rozpoczecia")
    private LocalDateTime faktycznaDataRozpoczecia;

    @JsonProperty("faktyczna_data_zakonczenia")
    private LocalDateTime faktycznaDataZakonczenia;

    @JsonProperty("przebieg_rozpoczecia")
    private int przebiegRozpoczecia;

    @JsonProperty("przebieg_zakonczenia")
    private int przebiegZakonczenia;

    @JsonProperty("id_pracownika")
    private Long idPracownika;

    @JsonProperty("id_pojazdu")
    private Long idPojazdu;

    @JsonProperty("status_wypozyczenia")
    private Wypozyczenie.statusyWypozyczenia statusWypozyczenia;

Here's the Wypozyczenie class:

@Entity
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Wypozyczenie {

    //ENUM Type of statusyWypozyczenia
    public enum statusyWypozyczenia{
        ZAREZERWOWANE, WYPOZYCZONE, ZAKONCZONE;
        public static final EnumSet<statusyWypozyczenia> allStatusyWypozyczenia = EnumSet.allOf(statusyWypozyczenia.class);
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(columnDefinition = "datetime2")
    @JsonProperty("planowana_data_rozpoczecia")
    private LocalDateTime planowanaDataRozpoczecia;
    @Column(columnDefinition = "datetime2")
    @JsonProperty("planowana_data_zakonczenia")
    private LocalDateTime planowanaDataZakonczenia;
    @Column(columnDefinition = "datetime2")
    @JsonProperty("faktyczna_data_rozpoczecia")
    private LocalDateTime faktycznaDataRozpoczecia;
    @Column(columnDefinition = "datetime2")
    @JsonProperty("faktyczna_data_zakonczenia")
    private LocalDateTime faktycznaDataZakonczenia;

    @JsonProperty("przebieg_rozpoczecia")
    private int przebiegRozpoczecia;

    @JsonProperty("przebieg_zakonczenia")
    private int przebiegZakonczenia;

    @ManyToOne(fetch=FetchType.LAZY)
    @JsonManagedReference
    private Pracownik pracownik;

    @ManyToOne(fetch=FetchType.LAZY)
    @JsonManagedReference
    private Pojazd pojazd;



    @Enumerated
    @Column(columnDefinition = "smallint")
    @JsonProperty("status_wypozyczenia")
    private statusyWypozyczenia statusWypozyczenia;

    @OneToMany(mappedBy = "wypozyczenie")
    @JsonBackReference
    private List<CzynnoscSerwisowa> czynnosciSerwisowe;

    @OneToMany(mappedBy = "wypozyczenie")
    @JsonBackReference
    private List<CzynnoscEksploatacyjna> czynnosciEksploatacyjne;

Here's the main for main app, you can see how I initialize bean:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class ApiBdCarRentApplication {


    @Bean
    public DozerBeanMapper mDozerBeanMapper() {
        return new DozerBeanMapper();
    }

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

    public static void main(String[] args) {

        System.out.println(Integer.getInteger("4"));
        SpringApplication.run(ApiBdCarRentApplication.class, args);

    }


}

And here's the controller for Wypozyczenia class:

@RequestMapping("/wypozyczenia")
@RestController
public class WypozyczenieController {

    private WypozyczenieService mWypozyczenieService;
    private DozerBeanMapper mDozerBeanMapper;



    @Autowired
    public WypozyczenieController(WypozyczenieService wypozyczenieService, DozerBeanMapper dozerBeanMapper) {
        mWypozyczenieService = wypozyczenieService;
        mDozerBeanMapper = dozerBeanMapper;

    }

    @GetMapping("")
    public List<WypozyczenieDto> getAllWypozyczenia(){
        List<Wypozyczenie> wypozyczenia = mWypozyczenieService.getAllWypozyczenia();
        return wypozyczenia.stream()
                .map(wypozyczenie -> convertToDto(wypozyczenie))
                .collect(Collectors.toList());
    }

    private WypozyczenieDto convertToDto(Wypozyczenie wypozyczenie) {
        WypozyczenieDto wypozyczenieDto = mDozerBeanMapper.map(wypozyczenie, WypozyczenieDto.class);
        return wypozyczenieDto;
    }

Please help me. What is going on? Why isn't it working even on the simple conversion level (I've tried it on other classes).

I just tried doing the following with the method BeanUtils.copyProperties of Spring framework. Seems to be working fine. Couldn't test with LocalDateTime as I don't have Java 8. Tried with Date

public class BeanCopyTest{
    private static WypozyczenieDto convertToDto(Wypozyczenie wypozyczenie) {
       WypozyczenieDto wypozyczenieDto = new WypozyczenieDto(); 
       BeanUtils.copyProperties(wypozyczenie,wypozyczenieDto);
       return wypozyczenieDto;
    }

    public static void main(String[] args){
       Wypozyczenie wypozyczenie = new Wypozyczenie();
       wypozyczenie.setId((long)1);
       wypozyczenie.setPrzebiegRozpoczecia(2);
       wypozyczenie.setPrzebiegZakonczenia(3);
       wypozyczenie.setPlanowanaDataRozpoczecia(new Date());
       WypozyczenieDto wypozyczenieDto = convertToDto(wypozyczenie);
       System.out.println("Id = "+wypozyczenieDto.getId());
       System.out.println("Rozpoczecia = "+wypozyczenieDto.getPrzebiegRozpoczecia());
       System.out.println("Zakonczenia = "+wypozyczenieDto.getPrzebiegZakonczenia());
       System.out.println("PlanowanaDataRozpoczecia = "+wypozyczenieDto.getgetPlanowanaDataRozpoczecia());
    }
}

Printed the following result.

Id = 1
Rozpoczecia = 2
Zakonczenia = 3
PlanowanaDataRozpoczecia = Fri Feb 02 11:09:48 IST 2018

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM