Java 使用DTO的Post方法

Java 使用DTO的Post方法,java,hibernate,spring-boot,dto,Java,Hibernate,Spring Boot,Dto,我想使用DTO与Angular进行通信,但实际上它不起作用。我想创建POST请求,使用Dto模型将数据从应用程序添加到数据库 您可以在图片上看到我的错误: 我的班级客户: @Entity @Table(name = "customer") public class Customer { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int id; @Column(name = "name") private Stri

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

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

我的班级客户:

@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;
...
类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;
}
}
类CustomerController:

@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;
}
@Autowired
CustomerService customerService;

@PostMapping(value = "/customers/create")
public Customer postCustomer(@RequestBody CustomerDto customerDto) {
    Customer _customer = customerService.save(customerDto));
    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和TicketTo列表,您需要更改控制器上的方法调用,如下所示:

类CustomerController:

@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;
}
@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);
    }

对于实体DTO转换,我们需要使用ModelMapper或mapstruct库。 借助这些库,我们可以轻松地从Dto转换为实体,从实体转换为Dto对象。添加任何依赖项后,我们都可以使用它

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

在spring配置中定义ModelMapperbean

@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.maptargetClass、DestinationClass.class
我在这里使用了Java8代码,但是你可以使用任何人。我希望这将对您非常有帮助。

您正在尝试向列表ticketdto添加字符串值,并且您正在尝试将Customer构造函数用于此列表,但它期望List@vlad324谢谢,那么我如何用门票列表保存客户?@Viola您的customerservice保存方法接受CustomerTo CustomerTo,列出ticketdto,您正试图通过Customer@Viola您可以从TicketDTO创建票证列表,并将该列表添加到customer!谢谢,但它仍然不起作用,我应该改变我的角度部分,以结合Dto模型和我的前端?Erlier我只使用Customer类。不能将两个参数传递给控制器。从方法postCustomer中去掉List TicketTos,并从angular作为DTO@ChirdeepTomar非常感谢你,伙计!我在那里真的疯了!:@维奥拉,请再调查一下,我已经更新了答案!直到你从angular应用程序中通过正确的dto,它应该是好的!如果你想不出来,就把你的请求贴出来@YogenRai只有在CustomerServiceImpl中用列表票据对部件进行注释时,它才会起作用。所以只保存名称,因为保存列表对我不起作用。以下是我的新问题: