@GetMapping 不会在用户名下显示 CreditCards。 @PostMapping 不会为用户创建新卡,它只会更新它
@GetMapping doesn't display CreditCards under username. @PostMapping doesn't create a new card for user, it only updates it
我的用户 Class 如下所示:
@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
信用卡Class看起来:
@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(接口)和 CreditCardRepository),但这些不会影响我遇到的问题。请问如何修复在 url 上为传递用户名的用户获取卡片列表的问题。用户如何创建新的/多个信用卡而不是更新他拥有的信用卡。
您正在尝试使用您的用户 ID 获取信用卡
return cardsRepository.findById(userRepository.findByUsername(username).get().getUserID());
相反,您可以按用户搜索您的信用卡。为此,您应该在信用卡存储库界面中创建一个方法。
List<CreditCard> findByUser(User user);
然后从你的控制器调用这个方法
return cardsRepository.findByUser(userRepository.findByUsername(username).get())
post方法也有类似的问题。您正在尝试通过用户名获取用户,但传递的是用户 ID。您还可以将用户设置为您的新信用卡,但不会向您的用户添加新信用卡。 (并将用户 class 中的信用卡变量名称更改为 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,那么程序就会像我和试图成为的信用卡一样对待它已更新。
我的用户 Class 如下所示:
@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
信用卡Class看起来:
@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(接口)和 CreditCardRepository),但这些不会影响我遇到的问题。请问如何修复在 url 上为传递用户名的用户获取卡片列表的问题。用户如何创建新的/多个信用卡而不是更新他拥有的信用卡。
您正在尝试使用您的用户 ID 获取信用卡
return cardsRepository.findById(userRepository.findByUsername(username).get().getUserID());
相反,您可以按用户搜索您的信用卡。为此,您应该在信用卡存储库界面中创建一个方法。
List<CreditCard> findByUser(User user);
然后从你的控制器调用这个方法
return cardsRepository.findByUser(userRepository.findByUsername(username).get())
post方法也有类似的问题。您正在尝试通过用户名获取用户,但传递的是用户 ID。您还可以将用户设置为您的新信用卡,但不会向您的用户添加新信用卡。 (并将用户 class 中的信用卡变量名称更改为
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,那么程序就会像我和试图成为的信用卡一样对待它已更新。