java如何正确组合@PathParams和@RequestBody?
在我的Spring MVC控制器中,我试图将输入参数映射到一个对象。我的控制器目前看起来是这样的:
@RestController("fundsConfirmationController")
@RequestMapping("/accounts/{accountId}/funds-confirmations")
public class FundsConfirmationController {
@GetMapping(
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<?> fundsConfirmation(@PathVariable("accountId") String accountId,
@RequestBody FundsConfirmationRequestDTO fundsConfirmationRequestDTO) {
System.out.println(accountId + " " + fundsConfirmationRequestDTO);
return null;
}
因此,除了在方法中单独设置accountId
之外,我还没有找到一种方法来正确组合@PathVariable
和@RequestBody
?(我无法更改传入参数,因为这些是预定义的要求。)
在同一个对象中是否有合适的方式组合@PathParams
和@ResponseBody
?不单独映射DTO中的路径参数
有什么建议可以妥善解决这个问题吗
如果我在错误的地方发帖,或者我需要详细说明,请纠正我
提前谢谢你,汤姆
# 1 楼答案
这里的问题是使用GET方法。如果希望Spring使用requestBody,那么就需要使用POST方法。在GET方法中,身体被忽略了
见: