dimanche 29 septembre 2019

@PostMapping - Angular + Spring Boot

For some reason I can't make a @PostMapping work, wierd thing is, other post is working.

Here are the 2 methods from Spring, the first is working, the second one is not working (By not working I mean that even the System print is not being called, it's like the server is not getting the request, while the second one does). I have no idea what is wrong, I tried entering the full URL - did not help. tried almost about everything.

@PostMapping(path = "", consumes = { "application/json" })
public void create(Authentication authentication, @RequestBody Coupon coupon) {
    System.out.println("Create was called");
    if (coupon != null) {
        // updating UserModel_Coupon
        UserModel temp = userRepository.findByUsername(authentication.getName());
        couponRepository.save(coupon);
        temp.createCoupon(coupon);
        userRepository.save(temp);
    }
}

@PostMapping(path = "/buy", consumes = { "application/json" })
public void buy(Authentication authentication, @RequestBody Coupon coupon) {
    System.out.println("Purchuse was called");
    if (userRepository.findByUsername(authentication.getName()).getRoles().equals("CUSTOMER")
            && coupon.getAmount() >= 1) {
        UserModel temp = userRepository.findByUsername(authentication.getName());
        Coupon tempc = coupon;
        temp.createCoupon(tempc);
        tempc.setAmount(tempc.getAmount() - 1);
        couponRepository.save(tempc);
        userRepository.save(temp);
    }
}

the angular code - I am getting the console log for both of them, and both of the logs are responding - again, the first one is working, the second one won't work (the console.log is working, and I'm getting the JSON of the 'coupon' object).

 createCoupon(coupon) {
    console.log("Create request sent")
    return this.httpClient.post<Coupon>(`${API_URL}/coupon`, coupon)
  }

buyCoupon(coupon) {
    console.log(coupon)
    return this.httpClient.post<Coupon>(`${API_URL}/coupon/buy`, coupon)
  }



Aucun commentaire:

Enregistrer un commentaire