2016-07-22 147 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; 

希望这有助于