@GetMapping 不会在用户名下显示信用卡。@PostMapping不会为用户创建新卡片,只会更新它



我的用户类如下所示:

@Entity
@Table(name = "Users")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userID")
private Integer userID;
@Column(name = "username",nullable = false, unique = true)
private String username;
@Column(name = "password")
private String password;
@Column(name = "name")
private String name;
@Column(name = "address")
private String address;
@Column(name = "email")
private String email;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<CreditCard> creditCard;
//Constructor, Getters and Setters

信用卡类别外观:

@Entity
@Table(name = "CreditCards")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class CreditCard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "cardID", nullable = false)
private Integer cardID;
@Column(name = "cardName")
private String cardName;
@Column(name = "cardNumber")
private BigInteger cardNumber;
@Column(name = "expirationDate")
private Integer expirationDate;
@Column(name = "securityCode")
private Integer securityCode;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "user_id", nullable = false)
@JsonIgnore
private User user;
//Constructor, Getters and Setters

信用卡资源:

@RestController
@RequestMapping("/geektext/users")
class CreditCardResource {
@Autowired
CreditCardRepository cardsRepository;
@Autowired
UserRepository userRepository;
//Displays CreditCard By Username Search
@GetMapping("/{username}/cards")
public Optional<CreditCard> getCardsByUsername(@PathVariable String username) throws NotFoundException {
if (!userRepository.findByUsername(username).isPresent()){
throw new NotFoundException("User '" + username + "' not found");
}
return cardsRepository.findById(userRepository.findByUsername(username).get().getUserID());
}
//Creates New Card for User
@PostMapping("/{userID}/cards")
public CreditCard loadCard(@PathVariable String userID, @RequestBody CreditCard creditCard) throws NotFoundException {
return userRepository.findByUsername(userID).map(user -> {creditCard.setUser(user);
return cardsRepository.save(creditCard);
}).orElseThrow(() -> new NotFoundException("User '" + userID + "' not found"));
}
}

还有一个UserResource.java、UserRepository(Interface(和CreditCardRepository(,但这些都不会影响我遇到的问题。请告诉我如何修复在url上传递用户名的用户的卡列表。用户如何创建新的/多个信用卡,而不是更新他拥有的信用卡。

  1. 您正试图使用您的userID 获得信用卡

    return cardsRepository.findById(userRepository.findByUsername(username).get().getUserID());
    

    相反,您可以按用户搜索您的信用卡。为此,您应该在信用卡存储库界面中创建一个方法。

    List<CreditCard> findByUser(User user);
    

    然后从控制器调用此方法

    return cardsRepository.findByUser(userRepository.findByUsername(username).get())
    
  • post方法也有类似的问题。您试图通过用户名获取用户,但传递了userID。此外,您还将用户设置为新的信用卡,但不会向用户添加新的信用证。(并将User类中信用卡变量的名称更改为creditCards(

    return userRepository.findByUsername(userID).map(user -> {creditCard.setUser(user);
    return cardsRepository.save(creditCard);
    }).orElseThrow(() -> new NotFoundException("User '" + userID + "' not found"));
    

    这样会好得多。你自己测试一下,如果我写错了,就修改一下

    User user = userRepository.findById(userID);
    user.getCreditCards().add(creditCard);
    creditCard.setUser(user);
    userRepository.save(user);
    

    NotFoundException我想你可以自己处理。

  • 更新:我必须为每张信用卡创建一个ID,因为如果在创建每张新信用卡时分配了相同的ID,那么程序会将其视为试图更新的同一张。

    相关内容

    • 没有找到相关文章

    最新更新