javaspring-bootgetbad-requestopenfeign

Getting a 400 Bad Request on a valid GET url from OpenFeign Client


My @EnableFeignClients Spring (3.1.5) application is puzzling me.

POM:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <!-- Required to use PATCH over default HttpURLConnection -->
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-hc5</artifactId> 
        </dependency>

I have the following config (CustomInterceptor is an @Component by itself) that does not use @Configuration as it will interfere with other clients.

@Import(FeignClientsConfiguration.class)
public class SoConfig {

    @Bean
    public RequestInterceptor intercept(CustomInterceptor interceptor) {
        return interceptor;
    }
}

Which is referred to from the following client:

@FeignClient(name = "so-client", url = "${config.url}", configuration = SoConfig.class)
public interface SoClient {

    @GetMapping(value = "?$filter=Identifier eq '{param}'")
    SoData getSoData(@PathVariable("param") String param);
}

And executed from the following gateway:

@Component
@RequiredArgsConstructor
public class SoGateway {

    private final SoClient client;

    public SoData getSoData(@NonNull String param) {
        try {
            return client.getSoData(param);
        } catch (FeignException e) {
            // handling
        }
    }
}

Upon runtime execution I get the following error:

Caused by: feign.FeignException$BadRequest: [400 Bad Request] during [GET] to [https://resolved-config-url?%24filter=Identifier%20eq%20'testParam'] [SoClient#getSoData(String)]: [<!DOCTYPE html>
<html>
<body>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
</body>
</html>
]
  1. When I copy url https://resolved-config-url?%24filter=Identifier%20eq%20'testParam' and GET it through Postman with a Bearer Token I get the desired response (url encoded or decoded, both work against this SAP system).
  2. When I do the same through Postman without a Bearer Token (thinking maybe the Interceptor didn't wire in) I get a message I need to provide a JWT (as expected).

I have been bashing my head against this problem for a fair few hours, please send an adult.

What I've tried (among other things I am now forgetting) -->


Solution

  • TLDR: we triggered a component scan that set multiple headers on the same request.


    Longer answers if anyone ever struggles with this, which I doubt, here's the highly specific answer that was plaguing the above.

    We added the @EnableFeignClients annotation on our main application (which was required for config processing), which triggered a component scan (and subsequent autowiring) for certain components - read: request interceptors - that were previously used on manual feign clients.

    This resulted in multiple Authorization headers being added to the RestTemplate request eventually throwing the 400 Bad Request upon firing.

    I would have really liked to see a clearer error message response (we tried with trace/full logging, never did we see more info) than the above, but there you have it.