2017년 2월 10일 금요일

RestTemplate proxy setting by active profile each

You can set the proxy to use RestTemplate.

- RestLoggingApplication.java

package org.blog.test;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.HashSet;

@SpringBootApplication
@Slf4j
public class RestLoggingApplication {
    public static void main(String[] args) {
        SpringApplication.run(RestLoggingApplication.class, args);
    }

    @Autowired
    private Environment environment;

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        if (new HashSet<>(Arrays.asList(environment.getActiveProfiles())).contains("local")) {
            log.info("set local proxy");
            httpClientBuilder.setProxy(new HttpHost("proxy.example.com", 8080, null));
        }
        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClientBuilder.build()));
        return restTemplate;
    }
}


- Result

2017-02-11 01:49:31.316  INFO 2040 --- [           main] org.blog.test.RestLoggingApplication     : set local proxy
2017-02-11 01:49:32.139  INFO 2040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1372ed45: startup date [Sat Feb 11 01:49:28 KST 2017]; root of context hierarchy
2017-02-11 01:49:32.203  INFO 2040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[GET]}" onto public java.lang.String org.blog.test.restcall.controller.RestTestController.testPostRest()
2017-02-11 01:49:32.203  INFO 2040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity
org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-02-11 01:49:32.203  INFO 2040 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-02-11 01:49:32.241  INFO 2040 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-11 01:49:32.241  INFO 2040 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-11 01:49:32.288  INFO 2040 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-11 01:49:32.457  INFO 2040 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-02-11 01:49:32.541  INFO 2040 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-11 01:49:32.541  INFO 2040 --- [           main] org.blog.test.RestLoggingApplication     : Started RestLoggingApplication in 4.93 seconds (JVM running for 5.699)

댓글 없음 :

댓글 쓰기