Search code examples
javaspringspring-bootkotlinspring-cloud

Account for @RequestBody parameter when using spring-cloud-gateway


I have the following Spring-Cloud-Gateway configuration and controller endpoint targeted by that configuration but my @RequestBody parameter is always null when the request reaches the endpoint. Could you please direct me as to how I should modify my Spring-Cloud-Gateway configuration, so that problem does not persist?

Configuration:::

---->>>

@Configuration
public class SpringCloudConfig {
    @Autowired SlotWalletDiscoveryService slotWalletDiscoveryService;

    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                  .route(r -> r.path("/api/bank/slot/account/**") //intercept calls to
                               .uri("http://localhost:8761/") //forward to
                               .id("employeeModule"))
                  .build();
    }

   @Bean
   public ServerCodecConfigurer serverCodecConfigurer() {
       return ServerCodecConfigurer.create();
   }
}



Endpoint:::

--->>>

@RestController
@RequestMapping("/api/bank/slot/account/")
class BankRequestController() {

    @GetMapping("/{ref}") fun test2(@PathVariable("ref") userRef: String, @RequestBody 
        bankRequest: BankRequest?, @RequestHeader(HttpHeaders.AUTHORIZATION)  authHeader : String): String? {
        return "Hello JavaInUse Called in First Service $userRef"
}



Debugger:::

--->>>

enter image description here

This is a good example but I would appreciate if someone could show me how to adapt it for my case scenario.

https://github.com/spring-cloud/spring-cloud-gateway/blob/master/spring-cloud-gateway-sample/src/main/java/org/springframework/cloud/gateway/sample/GatewaySampleApplication.java

Ive tried this with no success:

@Bean

public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
                  .route(r -> r.path("/api/bank/slot/account/**").and()
        .readBody(BankRequest.class, bankRequest -> {
                      return true;
                  }) //intercept calls to
                               .uri("http://localhost:8761/") //forward to
                               .id("employeeModule"))
                  .build();
}

Solution

  • The problem had nothing to do with the spring-cloud-gateway configuration. On Insomnia I had to specify

    enter image description here