Guest User

Untitled

a guest
Oct 23rd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. @SpringBootApplication
  2. public class AysRestClientApplication {
  3.  
  4. private static final Logger log = LoggerFactory.getLogger(AysRestClientApplication.class);
  5.  
  6.  
  7. public static void main(String[] args) {
  8. ApplicationContext ctx = SpringApplication.run(AysRestClientApplication.class, args);
  9. RestClient cl = ctx.getBean(RestClient.class);
  10. cl.makeCall();
  11. }
  12. }
  13.  
  14. @Service
  15. public class GetChangesMClient {
  16.  
  17. private final RestTemplate restTemplate;
  18. private final String url = "https://sample.org/restapi";
  19.  
  20. public GetChangesMClient(RestTemplateBuilder restTemplateBuilder) {
  21. List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();
  22. interceptors.add(new HeaderRequestInterceptor("Accept", MediaType.APPLICATION_JSON_VALUE));
  23.  
  24. this.restTemplate = restTemplateBuilder.basicAuthorization("username", "password").build();
  25. this.restTemplate.setInterceptors(interceptors);
  26. }
  27.  
  28. public GetChangesMResponseTO execute(GetChangesMRequestTO request) {
  29. return this.restTemplate.postForObject(url, request, GetChangesMResponseTO.class);
  30.  
  31. }
  32.  
  33. }
  34.  
  35. class HeaderRequestInterceptor implements ClientHttpRequestInterceptor {
  36.  
  37. private final String headerName;
  38. private final String headerValue;
  39.  
  40. public HeaderRequestInterceptor(String headerName, String headerValue) {
  41. this.headerName = headerName;
  42. this.headerValue = headerValue;
  43. System.out.println("Setting Interceptors");
  44. }
  45.  
  46. @Override
  47. public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
  48. System.out.println("intercepted");
  49. request.getHeaders().set(headerName, headerValue);
  50. return execution.execute(request, body);
  51. }
  52.  
  53. }
  54.  
  55. @Service
  56. public class RestClient {
  57.  
  58. @Autowired
  59. private GetChangesMClient client;
  60.  
  61. public void makeCall(){
  62. RequestTO request = new RequestTO();
  63.  
  64.  
  65. ResponseTO response = this.client.execute(request);
  66.  
  67. }
  68. }
Add Comment
Please, Sign In to add comment