2016-07-22 150 views
0

使用@Bean我想定義RestTemplate如使用我的配置類@Bean標註在春季啓動應用程序的應用程序配置豆休息模板。如何創建或在春季啓動

我打電話在不同的地方休息4個服務於我的應用程序流。目前我每次請求時都會創建RestTemplate。有沒有一種方法可以將應用程序bean定義爲使用@Bean並使用@Autowired注入?

對這個問題的主要原因是,我可以能夠使用@Bean定義RestTemplate但是當我@Autowired注入它,我失去了所有定義攔截(攔截器沒有得到調用。)

配置類

@Bean(name = "appRestClient") 
public RestTemplate getRestClient() { 

    RestTemplate restClient = new RestTemplate(
     new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); 

    List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>(); 
    interceptors.add(new RestServiceLoggingInterceptor()); 
    restClient.setInterceptors(interceptors); 

    return restClient; 
} 

服務類

public class MyServiceClass { 

    @Autowired 
    private RestTemplate appRestClient; 

    public String callRestService() { 
     // create uri, method response objects 
     String restResp = appRestClient.getForObject(uri, method, response); 
     // do something with the restResp 
     // return String 
    } 
} 

看來我Interceptors沒有得到調用此配置在所有。但是RestTemplate能夠調用REST服務並獲得響應。

+0

你確定你注入相同'RestTemplate'例如,您可能正在撿一些其他豆嗎?嘗試在'@ Autowired'註記旁邊的'org.springframework.beans.factory.annotation.Qualifier'中添加'@Qualifier(「appRestClient」)''。 – Edd

+0

感謝您的輸入daniel.When我試過@Qualifier攔截器沒有得到拾起。我想我在這裏失去了一些東西。 – springbootlearner

回答

2

來看形成攔截器的名字,我猜你正在做它的一些日誌?你可能錯過了日誌級別配置。我使用1.3.6.RELEASE版本創建了一個小應用程序來檢查您的配置是否工作。

在這個類中,我定義了RestTemplate豆和日誌記錄攔截器。

package com.example; 

// imports... 

@SpringBootApplication 
public class TestApplication { 

    private static final Logger LOGGER = LoggerFactory.getLogger(TestApplication.class); 

    public static void main(String[] args) { 
     SpringApplication.run(TestApplication.class, args); 
    } 

    @Bean(name = "appRestClient") 
    public RestTemplate getRestClient() { 
     RestTemplate restClient = new RestTemplate(
       new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())); 

     // Add one interceptor like in your example, except using anonymous class. 
     restClient.setInterceptors(Collections.singletonList((request, body, execution) -> { 

      LOGGER.debug("Intercepting..."); 
      return execution.execute(request, body); 
     })); 

     return restClient; 
    } 
} 

對於登錄工作,我還必須在application.properties中設置正確的調試級別。

logging.level.com.example=DEBUG 

然後,我創建,我這個注入一個RestTemplate服務。

@Service 
public class SomeService { 

    private final RestTemplate appRestClient; 

    @Autowired 
    public SomeService(@Qualifier("appRestClient") RestTemplate appRestClient) { 
     this.appRestClient = appRestClient; 
    } 

    public String callRestService() { 
     return appRestClient.getForObject("http://localhost:8080", String.class); 
    } 
} 

還有一個端點來測試這一點。

@RestController 
public class SomeController { 

    private final SomeService service; 

    @Autowired 
    public SomeController(SomeService service) { 
     this.service = service; 
    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String testEndpoint() { 
     return "hello!"; 
    } 

    @RequestMapping(value = "/test", method = RequestMethod.GET) 
    public String test() { 
     return service.callRestService(); 
    } 
} 

通過執行GET請求http://localhost:8080/test我應該會得到字符串hello!得到印刷(該服務對http://localhost:8080返回hello!,併發送回給我打電話)。帶有記錄器的攔截器也在控制檯中打印出Intercepting...

+0

謝謝埃德讓我試試這種方式。我使用的是春季啓動1.2.4 – springbootlearner

+0

我已經嘗試過上面的配置它工作正常。謝謝。 – springbootlearner

0

如果你使用Spring 1.4.0引導或更高版本埃德的解決方案將無法工作。你將不得不使用RestTemplateBuilder來使這個工作。這裏是例子

@Bean(name="simpleRestTemplate") 
@Primary 
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder){ 

    RestTemplate template = restTemplateBuilder.requestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory())) 
               .interceptors(logRestRequestInterceptor) //This is your custom interceptor bean 
               .messageConverters(new MappingJackson2HttpMessageConverter()) 
               .build(); 
    return template; 


} 

現在你可以在bean自動裝配到您的服務類

@Autowired 
@Qualifier("simpleRestTemplate") 
private RestTemplate simpleRestTemplate; 

希望這有助於