1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

One attribute is not transferred when passing an object to the backend, while others are

Discussão em 'Angular' iniciado por Sidharth Manikandan, Outubro 10, 2024 às 09:23.

  1. I am trying to make a E-Commerce Website and am using Angular for the frontend and SpringBoot for the backend. When a purchase is made, I want to save the details of the purchase in the database( I'm using MySQL). So once a purchase is done, the angular code passes an "purchase" object through POST to backend where my springboot controller recieves the purchase object and extracts the necessary information. The issue is that, even though the object has 4 properties, i,e, customer, address, order and orderItems, the addreess always shows up as null.

    This is my angular service code which sends the purchase object -

    export class CheckoutService {

    private purchaseUrl = "http://localhost:8080/api/checkout/purchase";

    constructor(public httpClient: HttpClient) { }

    placeOrder(purchase: Purchase): Observable<any> {
    return this.httpClient.post<Purchase>(this.purchaseUrl, purchase);
    }

    }


    I have verified that the purchase object does contain the address data at this point.

    Here is my Springboot controller code -

    @CrossOrigin("http://localhost:4200")
    @RestController
    @RequestMapping("/api/checkout")
    public class CheckoutController {

    private CheckoutService checkoutService;

    @Autowired
    public CheckoutController(CheckoutService checkoutService) {
    this.checkoutService = checkoutService;
    }

    @PostMapping("/purchase")
    public PurchaseResponse placeOrder(@RequestBody Purchase purchase){

    PurchaseResponse purchaseResponse = checkoutService.placeOrder(purchase);
    System.out.println(purchase);

    return purchaseResponse;
    }

    }


    Here, when the purchase object is checked, the address is always null. The other fields all perfectly work.

    Classes Purchase and Address in Angular -

    export class Purchase {

    customer! : Customer;
    address! : Address;
    order! : Order;
    orderItems!: OrderItem[];

    }


    export class Address {

    street! : string;
    city! : string;
    state! : string;
    country! : string;
    zipCode! : string;

    }


    Classes Purchase and Address in SpringBoot-

    @Data
    public class Purchase {

    private Customer customer;
    private Address shippingAddress;
    private Order order;
    private Set<OrderItem> orderItems;

    }

    @Entity
    @Table(name = "address")
    @Getter
    @Setter
    public class Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

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

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

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

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

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

    @OneToOne(mappedBy = "shippingAddress")
    private Order order;
    }

    Continue reading...

Compartilhe esta Página