Spring Boot using micro RestTemplate call other services

RestTemplate located spring-webmodule, org.springframework.web.clientunder the package, the Spring synchronization client HTTP access core classes, it forces the use of Restful principles to simplify and interactive HTTP server-side. It handles HTTP connection, separate application code to provide the URL (may take a few template variables) and extraction results.

Note: By default, RestTemplete rely on the standard JDK tools to establish an HTTP connection, you can setRequestFactoryattribute to use different HTTP library to replace him, such as Apache HttpComponent, Netty and OkHttp.

 

 

 

Further, exchangeand executemethods are generic versions of the above process can be used to support additional less frequently used in combination (for example: HTTP PARCH, the HTTP response body carrying the PUT), but note that, based HTTP library used must support the required The combination.

Each HTTP method have three variants: two receiving template string of a URI and URI variables (arrays, and variables), receiving a third URI, please note that, for the URI template, Jiading must be encoded. eg:  restTemplate.getForObject("https://example.com/hotel list")becomes https://example.com/hotel%20list this also means that if URI or URI template variables have been coded, dual coding will occur eg:  https://example.com/hotel%20listbecome https://example.com/hotel%2520listTo avoid this, use the URImethod variants to provide (or reuse) before coding the URI. To prepare for full control of encoding URI, consider using org.springframework.web.util.UriComponentsBuilder.

In the internal template HttpMessageConverterinstance HTTP messages back and forth between POJO. By default, by setMessageConvertersregistered major mime type of converter, but you can sign up for other converters.

The template uses org.springframework.http.client.SimpleClientHttpRequestFactoryand DefaultResponseErrorHandleras separate HTTP connection or create a default HTTP error handling strategy. It can be respectively setRequestFactoryand setErrorHandleroverride these defaults.

Demo

Maven relies introduced

<properties>
<!-- JDK版本 -->
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.22.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.5.22.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

Create a Message Entity class

public class MsgBean {
private int id;
private String name;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "MsgBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

Create a master class run SpringBoot

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

 

Creating RestTemplate configuration class, target acquisition TestTemplate

@Configuration
public class RestTemplateConfig {
    @Bean
    public Residual Template rest template (Http Client Request Factory factory) {
         return  new Rest Template (factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//5秒
        factory.setConnectTimeout(15000);//15秒
        return factory;
    }
}

 

Create a service class is called ServerController, to simulate a micro service, where restful API calls

/**
* Created by fubin on 2019/9/24.
* Invoked controller
*/

@RestController
@RequestMapping("server")
public class ServerController {

@GetMapping("/get")
public MsgBean get(){
MsgBean msgBean = new MsgBean();
msgBean.setId(1);
msgBean.setName("msg1");
return msgBean;
}

@PostMapping("/post")
public MsgBean post(){
MsgBean msgBean = new MsgBean();
msgBean.setId(1);
msgBean.setName("msg1");
return msgBean;
}

/**
* Post method by value
* @param id
* @param name
* @return
*/
@PostMapping("/postParam")
public String postParam(@RequestParam("id") String id, @RequestParam("name") String name){
System.out.println("Post id:"+id);
System.out.println("Post name:"+name);
return "post succ";
}


/**
* Post method by value
* @param id
* @param name
* @return
*/
@PutMapping("/put")
public String put(@RequestParam("id") String id,@RequestParam("name") String name){
System.out.println("put id:"+id);
System.out.println("put name:"+name);
return "del succ";
}


/**
* Del method by value
* @param id
* @return
*/
@DeleteMapping("/del")
public String del(@RequestParam("id") String id){
System.out.println("del id:"+id);
return "del succ";
}


}

 

Create a service call class InvokeController, by calling ServerController service RestRemplate

Guess you like

Origin www.cnblogs.com/fubinhnust/p/11930744.html