Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

background

The SpringBoot project framework will integrate a unified remote call solution to access the interfaces of other services.

For example, Http interfaces, WebService interfaces, and other services exposed interfaces.

The RestTemplate component is integrated for the SpringBoot environment, and the related tool classes are generated to improve the development efficiency.

The specific deployment of the implementation

Again, the corresponding JAR package is introduced into the POM file first.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code

Don’t be too curious as to why you don’t include a JAR package for RestTemplate, since the starter-Web dependency already does by default.

With this package in place, we can now assemble properties in the corresponding class as follows:

@Autowired
private RestTemplate restTemplate;
Copy the code

This way you can call the relevant methods, or you can simply new a RestTemplate object.

extension

Integrating with RestTemplate is not just about introducing a JAR package, but more about customizing your project. I’ve introduced an auto-configuration class here, and I’ve posted the code for reference.

@ Configuration public class RestTemplateAutoConfiguration {/ * * * * / @ connection timeout time Value (" ${rest. Connection. The timeout} ") private  Integer connectionTimeout; */ @value ("${rest.read.timeout}") private Integer readTimeout; @return RestTemplate */ @bean public RestTemplate registerTemplate(ClientHttpRequestFactory simileClientHttpRequestFactory) { RestTemplate restTemplate = new RestTemplate(); // Configure a custom message converter List<HttpMessageConverter<? >> messageConverters = restTemplate.getMessageConverters(); messageConverters.add(new RestTemplateHttpMessageConverter()); restTemplate.setMessageConverters(messageConverters); / / configure custom interceptor interceptor List < ClientHttpRequestInterceptor > interceptors = new ArrayList < > (); interceptors.add(new HeadClientHttpRequestInterceptor()); interceptors.add(new TrackLogClientHttpRequestInterceptor()); restTemplate.setInterceptors(interceptors); / / configure a custom exception handling restTemplate setErrorHandler (new RestTemplateExceptionHandle ()); restTemplate.setRequestFactory(simileClientHttpRequestFactory); return restTemplate; } / * * * * @ initialization request factory return SimpleClientHttpRequestFactory * / @ Bean public SimpleClientHttpRequestFactory getFactory () { SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(connectionTimeout); factory.setReadTimeout(readTimeout); return factory; } / * * * * / static data converter class RestTemplateHttpMessageConverter extends MappingJackson2HttpMessageConverter {public RestTemplateHttpMessageConverter() { List<MediaType> mediaTypes = new ArrayList<>(); mediaTypes.add(MediaType.TEXT_PLAIN); mediaTypes.add(MediaType.TEXT_HTML); // Added support for text/ HTML types setSupportedMediaTypes(mediaTypes); // tag6 } } }Copy the code

Of course, it also needs to be configured with parameters in application.yml, as shown in the code below.

rest:
  connection:
    timeout: 10000
  read:
    timeout: 20000
Copy the code