普通格式:
@RestController
@RequestMapping("/api")
public class PaymentHistoryController {
@Autowired
private paymentHistoryService paymentHistoryService;
@PutMapping("/payment")
public R processPaymentRequest(@RequestHeader("Authorization") String authorizationHeader,
@RequestBody PaymentRequest paymentRequest) {
try {
String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
CheckResult checkResult = JwtUtils.validateJWT(token);
if (checkResult.isSuccess()) {
Claims claims = checkResult.getClaims();
int userId = Integer.parseInt(claims.getId());
String username = claims.getSubject();
paymentRequest.getPaymentHistory().setUid(userId);
paymentRequest.getPaymentHistory().setUsername(username);
paymentRequest.getPaymentHistory().setPaymentDate(LocalDateTime.now());
paymentRequest.getLoaning().setUid(userId);
boolean paymentInserted = paymentHistoryService.insertPaymentHistory(paymentRequest.getPaymentHistory());
boolean debtUpdated = paymentHistoryService.updateDebtAmountByUid(userId,paymentRequest.getPaymentHistory().getPaymentAmount());
if (paymentInserted && debtUpdated) {
System.out.println("用户名是:"+username);
return R.ok("还款成功");
} else {
return R.error("还款失败");
}
} else {
return R.error("Token验证失败");
}
} catch (Exception e) {
System.out.println("剩余金额是:"+paymentRequest.getPaymentHistory().getPaymentAmount());
return R.error("处理请求时发生错误:" + e.getMessage());
}
}
}
json格式:
{
"loaning": {
"username": "力士山",
"amount": 100.0
},
"paymentHistory": {
"paymentAmount": 100.0
}
}
优化:
现在请求体的JSON格式只包含了 "amount" 字段。在你的代码中,你需要从JWT中解析出用户名,并将这个用户名与传入的 "amount" 值一起使用来执行操作。
在代码中,使用了 claims.getSubject()
来获取用户名,这通常是一个不错的选择,但是请确保在创建JWT令牌时也将用户名放入了 sub
(subject) 声明中
@RestController
@RequestMapping("/api")
public class PaymentHistoryController {
@Autowired
private paymentHistoryService paymentHistoryService;
@PutMapping("/payment")
public R processPaymentRequest(@RequestHeader("Authorization") String authorizationHeader,
@RequestBody Map<String, Double> requestBody) {
try {
String token = authorizationHeader.substring(7); // 去掉 "Bearer " 前缀
CheckResult checkResult = JwtUtils.validateJWT(token);
if (checkResult.isSuccess()) {
Claims claims = checkResult.getClaims();
int userId = Integer.parseInt(claims.getId());
String username = claims.getSubject(); // 获取用户名
PaymentHistory paymentHistory = new PaymentHistory();
paymentHistory.setUid(userId);
paymentHistory.setUsername(username);
paymentHistory.setPaymentDate(LocalDateTime.now());
paymentHistory.setPaymentAmount(requestBody.get("amount"));
Loaning loaning = new Loaning();
loaning.setUid(userId);
boolean paymentInserted = paymentHistoryService.insertPaymentHistory(paymentHistory);
boolean debtUpdated = paymentHistoryService.updateDebtAmountByUid(userId, paymentHistory.getPaymentAmount());
if (paymentInserted && debtUpdated) {
System.out.println("用户名是:" + username);
return R.ok("还款成功");
} else {
return R.error("还款失败");
}
} else {
return R.error("Token验证失败");
}
} catch (Exception e) {
System.out.println("剩余金额是:" + requestBody.get("amount"));
return R.error("处理请求时发生错误:" + e.getMessage());
}
}
}
json:
{
"amount": 100.0
}
在这个代码中,请求体直接解析为一个 Map<String, Double>
,其中键是 "amount",值是请求体中的 "amount" 值。然后,根据JWT解析出的用户名和请求体中的 "amount",创建相应的对象进行操作。
使用 Map<String, Double>
只是一种示范,用来解析请求体中的数据,并提取出你需要的部分。这种做法适用于请求体中只包含少量字段的情况,但并不是通用的做法。
在这个特定的情况下,你的代码需要解析请求体中的 "amount" 字段,并将其与JWT中解析出的用户名一起使用。而你之前的代码中使用了 PaymentRequest
类来封装请求体中的 Loaning
和 PaymentHistory
,但现在你只需要 "amount" 字段,所以并不需要再使用一个完整的对象。
使用 Map<String, Double>
的思路是,直接将请求体中的 JSON 数据解析为一个简单的键值对(key-value)结构,其中键是字段名(例如 "amount"),值是字段对应的值(例如 100.0)。在这种情况下,你可以使用 "amount"
这个键从 Map
中获取对应的值,然后将这个值与解析出的用户名一起使用。