简体   繁体   English

ModelMapper和DozerBeanMapper无法正常工作 Spring Boot REST API

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

I'm developing Spring Boot Rest Api. 我正在开发Spring Boot Rest Api。 I have class called "Wypozyczenie", and class called "WypozyczenieDTO". 我有一个名为“ WypozyczenieDTO”的课程,也有一个名为“ WypozyczenieDTO”的课程。 I want to extract some info from class Wypozyczenie (avoid nested objects) and return data based on info from WypozyczenieDTO. 我想从类Wypozyczenie(避免嵌套对象)中提取一些信息,并基于WypozyczenieDTO的信息返回数据。 I've tried DozerBeanMapper and ModelMapper and both don't convert my object properly. 我已经尝试过DozerBeanMapper和ModelMapper,但都不能正确转换我的对象。 All the values are null or zero! 所有值都为null或零! 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. 我正在调试,并且wypozyczenieDto中的所有参数均为null或零。

Here are the Wypozyczenie and WypozyczenieDto classes (without getters and setters): 这是Wypozyczenie和WypozyczenieDto类(没有getter和setter):

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: 这是Wypozyczenie类:

@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: 这是主要应用程序的主要内容,您可以看到如何初始化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: 这是Wypozyczenia类的控制器:

@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. 我只是尝试使用Spring框架的BeanUtils.copyProperties方法执行以下操作。 Seems to be working fine. 似乎工作正常。 Couldn't test with LocalDateTime as I don't have Java 8. Tried with Date 无法与测试LocalDateTime ,因为我没有安装Java 8.尝试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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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