2016-07-22 20 views
0

RestTemplateをアプリケーション起動時に設定する場合は、Springブートアプリケーションの設定クラスで@Bean注釈を使用します。Spring起動時に@Beanを使用してRestテンプレートを作成または構成する方法

私は、アプリケーションフローのさまざまな場所で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 @Qualifier("simpleRestTemplate") private RestTemplate simpleRestTemplate; 

あなたのサービスクラスにBeanをautowireことを願っています他の豆を拾っている? '@ Autowired'アノテーションの隣に' org.springframework.beans.factory.annotation.Qualifier'から '@Qualifier(" appRestClient ")'を追加してみてください。 – Edd

+0

あなたの入力danielに感謝します。私が@Qualifierインタセプタを試してみると、私はここで何かが見つからないと思います。 – springbootlearner

答えて

2

インターセプタの名前を判断すると、ログインしていると思いますか?ログレベルの設定を逃した可能性があります。1.3.6.RELEASEバージョンを使用して、設定が機能している天気を確認する小さなアプリケーションを作成しました。

このクラスでは、ログを使用してRestTemplateのBeanとインターセプタを定義します。ロギングが機能するためには

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); 
    } 
} 

また、これをテストするエンドポイントもあります。私はhello!がプリントなっ文字列を得ることを期待すべきであるhttp://localhost:8080/testからGET要求を(サービスがhello!を返し、私にこれを送り返すhttp://localhost:8080を呼び出します)を行うことにより

@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(); 
    } 
} 

。ロガーを備えたインターセプターは、Intercepting...をコンソールにも出力します。

+0

ありがとうEddはこの方法を試してみましょう。私は春の起動を使用しています1.2.4 – springbootlearner

+0

私はそれがうまく動作している上記の設定を試してみました。ありがとう。 – springbootlearner

0

Spring Boot 1.4.0以降を使用している場合、Eddのソリューションは動作しません。この作業を行うには、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; 


} 

が、これは

関連する問題