Java 如何正确组合@PathParams和@RequestBody?
在我的SpringMVC控制器中,我试图将输入参数映射到一个对象。我的控制器当前看起来如下所示:Java 如何正确组合@PathParams和@RequestBody?,java,spring-mvc,spring-boot,Java,Spring Mvc,Spring Boot,在我的SpringMVC控制器中,我试图将输入参数映射到一个对象。我的控制器当前看起来如下所示: @RestController("fundsConfirmationController") @RequestMapping("/accounts/{accountId}/funds-confirmations") public class FundsConfirmationController { @GetMapping( consumes = MediaType.APPLICA
@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;
}
@RestController(“资金确认控制器”)
@请求映射(“/accounts/{accountId}/funds confirmations”)
公共类基金确认控制员{
@GetMapping(
consumes=MediaType.APPLICATION\u JSON\u值,
products=MediaType.APPLICATION\u JSON\u值
)
公共响应资金确认(@PathVariable(“accountId”)字符串accountId,
@请求主体资金确认请求到资金确认请求到){
System.out.println(accountId+“”+fundsConfirmationRequestDTO);
返回null;
}
因此,除了在方法中单独设置accountId
之外,我还没有找到一种方法来正确组合@PathVariable
和@RequestBody
。(我无法更改传入参数,因为它们是预定义的要求。)
在同一个对象中是否有适当的方法组合
@PathParams
和@ResponseBody
?而不单独映射数据中的路径参数
有什么建议可以妥善解决这个问题吗
如果我张贴在错误的地方或我需要指定更多的细节,请纠正我
提前感谢Tom,这里的问题是您使用GET方法。如果您希望Spring使用requestBody,您需要使用POST方法。使用GET方法,body被忽略 见:
GET
请求有什么意义?根据设计,请求的额外参数以JSON格式添加到请求中。/accounts/{accountId}/资金确认{“amount”:1234.34,“ccy”:“EUR”,“cardname”:“1234”,“paye”:“Tom Br”}问题到底是什么?在同一个对象中是否有合适的方法组合“PathParams”和“ResponseBy”?而不单独映射DTO中的路径参数?感谢您的输入。GET方法能够接收@RequestBody。但是,通过查看您的资源,我对传入的参数设置提出了质疑。在GET请求中,t它们从JSON负载转换为查询参数。