Post 使用DTO的方法

Post Method using DTO

我想用DTO与Angular通信,但实际上不行。我想创建 POST 请求以使用 Dto 模型将数据从我的应用程序添加到数据库。

你可以在图片上看到我的错误:

我的class客户:

@Entity
@Table(name = "customer")
public class Customer {

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

@Column(name = "name")
private String name;

@OneToMany
private List<Ticket> ticket;
...

Class CustomerDto:

public class CustomerDto {
private String name;
private List<TicketDto> ticket;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public List<TicketDto> getTicket() {
    return ticket;
}

public void setTicket(List<TicketDto> ticket) {
    this.ticket = ticket;
}
}

Class 客户控制器:

@Autowired
CustomerService customerService;

@PostMapping(value = "/customers/create")
public Customer postCustomer(@RequestBody CustomerDto customerDto, List<TicketDto> ticketDtos) {
    //ArrayList<TicketDto> tickets = new ArrayList<>();
    ticketDtos.add(customerDto.getName());
    ticketDtos.add(customerDto.getTicket());
    Customer _customer = customerService.save(new Customer(customerDto.getName(), ticketDtos ));
    return _customer;
}

客户服务:

public interface CustomerService {

void save(CustomerDto customerDto, List<TicketDto> ticketDtos);
}

CustomerServiceImpl:

@Service
public class CustomerServiceImpl implements CustomerService {

@Autowired
CustomerRepository repository;

@Override
public void save(CustomerDto customerDto, List<TicketDto> ticketDtos) {
    Customer customer = new Customer();
    customer.setName(customerDto.getName());
    customer.setTicket(customerDto.getTicket());

    List<Ticket> tickets = new ArrayList<>();
    for (TicketDto ticketDto : ticketDtos) {
        Ticket ticket = new Ticket();
        ticket.setDestinationCity(ticketDto.getDepartureCity());
        ticket.setDestinationCity(ticketDto.getDestinationCity());
        tickets.add(ticket);
    }
}

由于您的 CustomerServiceImpl 使用 CustomerDto 和 TicketDto 列表,您需要更改控制器上的方法调用,如下所示:

Class 客户控制器:

@Autowired
CustomerService customerService;

@PostMapping(value = "/customers/create")
public Customer postCustomer(@RequestBody CustomerDto customerDto) {
    Customer _customer = customerService.save(customerDto));
    return _customer;
}

并将 CustomerServiceImpl 更新为:

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    CustomerRepository repository;

    // change save to return saved customer
    @Override
    public Customer save(CustomerDto customerDto) {
        Customer customer = new Customer();
        customer.setName(customerDto.getName());
        // customer.setTicket(customerDto.getTicket()); // remove this

        List<Ticket> tickets = new ArrayList<>();
        for (TicketDto ticketDto : customerDto.getTicketDtos) {
            Ticket ticket = new Ticket();
            ticket.setDestinationCity(ticketDto.getDepartureCity());
            ticket.setDestinationCity(ticketDto.getDestinationCity());
            tickets.add(ticket);
        }
        customer.setTickets(tickets); // add this to set tickets on customer
        return repository.save(customer);
    }

显然,您还需要更改界面:

public interface CustomerService {

     Customer save(CustomerDto customerDto);
}

对于entity-DTO的转换,我们需要使用ModelMapper或者mapstruct库。 在这些库的帮助下,我们可以轻松地将 Dto 转换为实体,将实体转换为 dto 对象。添加任何依赖后,我们就可以使用它了。

如何使用,让我们看看...

在 spring 配置中定义 modelMapper bean。

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

假设我们需要将 List 转换为 List obj 那么我们可以像这样简单地执行:

List<TicketDto> ticketDtos = .... //Suppose It is holding some data
List<Ticket> tickets = ticketDtos.stream()
                       .map(tkt-> mappper.map(tkt, ticket.class))
                       .collect(Collectors.toList());

mappper.map(targetClass, DestinationClass.class)使用起来非常简单 我在这里使用 Java8 代码,但您可以使用任何人。希望对您有所帮助。