Reputation: 6084
How can I call one spring REST controller from another whereby passing in the required injections?
ProtectPanController.java (Actual Rest controller doing the work)
@RestController
public class ProtectPanController {
private ProtectPanService protectPanService;
public ProtectPanController(ProtectPanService protectPanService) {
this.protectPanService = protectPanService;
}
@PostMapping(value = "/pan/protect", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<String> testPostMethod(@RequestBody ProtectPan protectPan) {
ResponseEntity response = protectPanService.sendProtectPanRequest(protectPan);
return response;
}
}
WORKS (Rest URL: http://localhost:9090/hosted-payments-webapp-1.0.0/pan/protect
)
Payload:
{
"paymentAccountNumber": "4111111111111111",
"tenderClass": "CreditCard"
}
Response:
{
"token": "4111110PASeK1111"
}
Pan3dsLookupController.java (this controller calls above controller)
@RestController
public class Pan3dsLookupController {
@RequestMapping(value = {"/pan/3dslookup"})
public String doProtectPanAndCmpiLookup(@RequestBody ProtectPanCmpiLookup protectPanCmpiLookup) {
ProtectPan protectPan = protectPanCmpiLookup.getProtectPan();
return "forward:/pan/protect";
}
}
ProtectPanCmpiLookup.java (a wrapper around the actual object)
public class ProtectPanCmpiLookup {
private ProtectPan protectPan;
public ProtectPan getProtectPan() {
return protectPan;
}
public void setProtectPan(ProtectPan protectPan) {
this.protectPan = protectPan;
}
}
DOES NOT WORK !! (Rest URL: http://localhost:9090/hosted-payments-webapp-1.0.0/pan/3dslookup
)
Payload:
{
"protectPan": {
"paymentAccountNumber": "4111111111111111",
"tenderClass": "CreditCard"
}
}
Response:
forward:/pan/protect
Upvotes: 0
Views: 671
Reputation: 4371
The reason why your code :
return "forward:/pan/protect";
works this way and give you that text:
forward:/pan/protect
is that you use RestController and response body of your method is String type:
@RequestMapping(value = {"/pan/3dslookup"})
public String <---
@RestController is a stereotype annotation that combines @ResponseBody and @Controller.
You have to return a ModelAndView object:
return new ModelAndView("forward:/pan/protect", modelName, modelObject)
Check this post for more info
Upvotes: 1