美文网首页
# java springcloud微服务模块之间接口调用方式(

# java springcloud微服务模块之间接口调用方式(

作者: yichen_china | 来源:发表于2021-10-13 13:56 被阅读0次

    配置类,配置RestTemplate Bean

    @Configuration
    public class RestTemplateConfig {
        
        
     
        /**
         * 使用LoadBlanced 可以用Eruake 中注册的服务名调用
         **/
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        
        
            return new RestTemplate(factory);
        }
     
        @Bean
        public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        
        
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setReadTimeout(5000);//单位为ms
            factory.setConnectTimeout(5000);//单位为ms
            return factory;
        }
     
    }
    
    注入RestTemplate 使用RestTemplate 调用接口
    @RequestMapping("/test2")
    public void test2(){
    
        try {
    
            // 调用的接口地址
            String url = "http://oscoreserver/test/test1";
            // 传的参数
            Map<String, Object> parameters = new HashMap<>(4);
            parameters.put("grant_type", "client_credential");
            // 接口返回的内容
            Object object = restTemplate.postForObject(url, parameters, String.class);
            System.out.println(object);
        }catch (Exception e){
    
            logger.info(e.getMessage());
            e.printStackTrace();
        }
    }
    
    
    RestTemplate Post请求
    • 使用postForObject请求接口
    String url = "http://oscoreserver/test/test1";
    Map<String, Object> parameters = new HashMap<>(4);
    parameters.put("tagid", "6666");
    Object object = restTemplate.postForObject(url, parameters, String.class);
    
    
    • postForObject()
    public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException {
    
        }
    public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables)  throws RestClientException {
    
        }
    public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException {
    
        }
    
    
    • postForEntity()
    <T> ResponseEntity<T> postForEntity(String var1, @Nullable Object var2, Class<T> var3, Object... var4) throws RestClientException;
    <T> ResponseEntity<T> postForEntity(String var1, @Nullable Object var2, Class<T> var3, Map<String, ?> var4) throws RestClientException;
    <T> ResponseEntity<T> postForEntity(URI var1, @Nullable Object var2, Class<T> var3) throws RestClientException;
    

    RestTemplate Get请求
    使用getForObject请求接口

    Map<String, Object> parameters = new HashMap<>(4);
    parameters.put("tagid", "6666");
    String result1 = restTemplate.getForObject(url, String.class, paramMap);
    System.out.println("result1====================" + result1);
    getForObject()
    public <T> T getForObject(String url, Class<T> responseType, Object... uriVariables){
        
        }
    public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables)
    public <T> T getForObject(URI url, Class<T> responseType)
    getForEntity()
    public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables){
        
        }
    public <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables){
        
        }
    public <T> ResponseEntity<T> getForEntity(URI url, Class<T> responseType){
        
        }
    

    相关文章

      网友评论

          本文标题:# java springcloud微服务模块之间接口调用方式(

          本文链接:https://www.haomeiwen.com/subject/dznsoltx.html