Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SpringBootApplication
- public class AysRestClientApplication {
- private static final Logger log = LoggerFactory.getLogger(AysRestClientApplication.class);
- public static void main(String[] args) {
- ApplicationContext ctx = SpringApplication.run(AysRestClientApplication.class, args);
- RestClient cl = ctx.getBean(RestClient.class);
- cl.makeCall();
- }
- }
- @Service
- public class GetChangesMClient {
- private final RestTemplate restTemplate;
- private final String url = "https://sample.org/restapi";
- public GetChangesMClient(RestTemplateBuilder restTemplateBuilder) {
- List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
- interceptors.add(new HeaderRequestInterceptor("Accept", MediaType.APPLICATION_JSON_VALUE));
- this.restTemplate = restTemplateBuilder.basicAuthorization("username", "password").build();
- this.restTemplate.setInterceptors(interceptors);
- }
- public GetChangesMResponseTO execute(GetChangesMRequestTO request) {
- return this.restTemplate.postForObject(url, request, GetChangesMResponseTO.class);
- }
- }
- class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {
- private final String headerName;
- private final String headerValue;
- public HeaderRequestInterceptor(String headerName, String headerValue) {
- this.headerName = headerName;
- this.headerValue = headerValue;
- System.out.println("Setting Interceptors");
- }
- @Override
- public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
- System.out.println("intercepted");
- request.getHeaders().set(headerName, headerValue);
- return execution.execute(request, body);
- }
- }
- @Service
- public class RestClient {
- @Autowired
- private GetChangesMClient client;
- public void makeCall(){
- RequestTO request = new RequestTO();
- ResponseTO response = this.client.execute(request);
- }
- }
Add Comment
Please, Sign In to add comment