3

I need a way to create a dynamic/generic rest client. My spring boot application need to connect to many third party clients, all of which have different request body, response body, some require special headers, while some require special authorization/authentication (e.g Basic Auth, JWT, HMAC etc).

So far I've managed to come up with the following client

public class GenericRestClient<T, V> {

    private RestTemplate restTemplate = new RestTemplate();

    public V execute(RequestDetails requestDetails, T data, ResponseErrorHandler errorHandler,
            Class<V> genericClass) throws ResourceAccessException, Exception {

        restTemplate.setErrorHandler(errorHandler);
        HttpHeaders headers = new HttpHeaders();

        HttpEntity<T> entity = new HttpEntity<T>(data, headers);
        ResponseEntity<V> response = restTemplate.exchange(requestDetails.getUrl(), requestDetails.getRequestType(),
                entity, genericClass);
        return response.getBody();
    }

}

But my question is. Is there a way now to generate all the necessary headers, authentication and authorization when all of them have different requirements? How can I do that?

Is there a way to store java code like a script (e.g., JTX) in the database and use them, or is there any better way to create my need?

I want it such that even if a new client comes, no further coding needs to be done.

1 Answer 1

1

You can store configuration of every client in database. If you use JPA you will end up with class like that:

@Entity
class RestClientConfiguration{
    private String url;
    private Map<String,String> headers;

    //whatever data you need
}

Then you only need to pass RestClientConfiguration to your GenericRestClient class.

public class GenericRestClient<T, V> {

    public V execute(RequestDetails requestDetails, T data, ResponseErrorHandler errorHandler,
            Class<V> genericClass, RestClientConfiguration clientConfiguration) throws ResourceAccessException, Exception {

        String url = clientConfiguration.getUrl();
        Map<String, String> headerMap = clientConfiguration.getHeaders();

        //here add code which map headerMap which is a Map to HttpHeaders object


        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setErrorHandler(errorHandler);
        HttpHeaders headers = new HttpHeaders();


        HttpEntity<T> entity = new HttpEntity<T>(data, headers);
        ResponseEntity<V> response = restTemplate.exchange(requestDetails.getUrl(), requestDetails.getRequestType(),
                entity, genericClass);
        return response.getBody();
    }
}

That's it. Hope I got your needs well. Let me know if something is not clear and we will work it out :)

10
  • Is there a way to do it such that, even if a new client comes, no code is needed to be done. If the config could be taken from the database or a script for that matter? I want it to be extremely dynamic and generic. Apr 18, 2020 at 6:09
  • I believe so. You can keep the configuration wherever you want and just set up RestTemplate Check out that tutorial: baeldung.com/spring-rest-template-builder Apr 18, 2020 at 6:13
  • But according to this I have to create a new rest template if a new configuration for a client shows up. Apr 18, 2020 at 7:29
  • Exactly, and do you have something against that? Apr 18, 2020 at 19:05
  • Well my team lead wants it to be such that, when a new client shows up, the changes must only be made in the database, and not in the code itself. Apr 19, 2020 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.