2

In my Spring MVC Controller I am trying to map incomming parameters to an object. My controller currently looks like this.:

@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;
}

As such I haven't found a way to properly combine @PathVariable and @RequestBody apart from setting the accountId seperatly in the method? (I can't change the incoming parameters as these are a predefined requirement.)

Is there a proper way of combining @PathParams and @ResponseBody in the same Object? Without to map the Path Param in the DTO seperatly?

Any suggestion how to properly tackle this?

In case I am posting in the wrong place or I need to specify more details please correct me.

Thanks in advance, Tom

Wojciech Wirzbicki
  • 2,990
  • 4
  • 27
  • 47
TomBrx
  • 199
  • 1
  • 15
  • 2
    What's the point of sending a *RequestBody* to a `GET` request? – Nicholas K Dec 18 '18 at 13:45
  • As per the design the extra parameters for the request are added in JSON to the request. /accounts/{accountId}/funds-confirmations { "amount":1234.34, "ccy":"EUR", "cardNumber":"1234", "payee":"Tom Br" } – TomBrx Dec 18 '18 at 13:47
  • What's the question exactly? – user7294900 Dec 18 '18 at 13:49
  • 1
    Is there a proper way of combining 'PathParams' and 'ResponseBody' in the same Object? Without to map the Path Param in the DTO seperatly? – TomBrx Dec 18 '18 at 13:50

1 Answers1

1

The issue here is that you use the GET method. If you want the requestBody to be used by Spring you want to use the POST method. With the GET method the body is just ignored.

See :

alain.janinm
  • 19,035
  • 10
  • 56
  • 103
  • 1
    Thanks for the input. The GET method was able to receive the @RequestBody. However by going through your resources I challenged the incoming setup of parameters. On the GET request they were converted from a JSON Payload to Query Parameters. – TomBrx Dec 19 '18 at 09:10