gpt4 book ai didi

java - 如何正确组合@PathParams和@RequestBody?

转载 作者:行者123 更新时间:2023-11-30 05:51:28 24 4
gpt4 key购买 nike

在我的 Spring MVC Controller 中,我试图将传入参数映射到一个对象。我的 Controller 目前看起来像这样:

@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 中的路径参数?

有什么建议如何正确解决这个问题吗?

如果我发布在错误的地方或者我需要指定更多细节,请纠正我。

提前致谢,汤姆

最佳答案

这里的问题是您使用了 GET 方法。如果您希望 Spring 使用 requestBody,则需要使用 POST 方法。使用 GET 方法时,正文将被忽略。

参见:

关于java - 如何正确组合@PathParams和@RequestBody?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53834341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com