zoukankan      html  css  js  c++  java
  • RestTemplate

    这篇文章打算介绍一下Spring的RestTemplate。我这边以前设计到http交互的,之前一直采用的是Apache HttpComponents 。后来发现Spring框架中已经为我们封装好了这个框架。因此我们就不需要直接使用下面这种稍微底层一点的方式来实现我们的功能:

    String uri = "http://example.com/hotels/1/bookings";
    
    PostMethod post = new PostMethod(uri);
    String request = // create booking request content
    post.setRequestEntity(new StringRequestEntity(request));
    
    httpClient.executeMethod(post);
    
    if (HttpStatus.SC_CREATED == post.getStatusCode()) {
        Header location = post.getRequestHeader("Location");
        if (location != null) {
            System.out.println("Created new booking at :" + location.getValue());
        }
    }

    Spring的RestTemplate提供了一些更高级别的方法来满足我们的功能,比如对HTTP Method的支持:

    虽然Spring的RestTemplate提供了对这么多HTTP method的支持,但是从个人工作角度来说,常用的也就get和post这两种方式,有兴趣的朋友可以自己翻看一下源码。

    RestTemplate的使用

    RestTemplate有两个构造方法,分别是:

    public RestTemplate() {
              /**
                   ...初始化过程
              */
    }
     
    public RestTemplate(ClientHttpRequestFactory requestFactory) {
         this();
         setRequestFactory(requestFactory);
    }

    其中,第二个构造方法中可以传入ClientHttpRequestFactory参数,第一个进行默认初始化,因为我们经常需要对请求超时进行设置并能 够对超时进行后续处理,而第一个构造方法,我们无法控制超时时间,第二个构造中的ClientHttpRequestFactory接口的实现类中存在 timeout属性,因此选用第二个构造方法。
    在spring配置文件中进行如下配置:

    <!-- 配置RestTemplate -->
             <!--Http client Factory-->  
            <bean id="httpClientFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">  
                <property name="connectTimeout"  value="${connectTimeout}"/>
                <property name="readTimeout"  value="${readTimeout}"/>
            </bean>  
               
            <!--RestTemplate-->  
            <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">  
                <constructor-arg ref="httpClientFactory"/>  
            </bean>

    当然也可以直接使用:

       SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(1000);
            requestFactory.setReadTimeout(1000);
    
            RestTemplate restTemplate = new RestTemplate(requestFactory);

    注意:ClientHttpRequestFactory 接口有4个实现类,分别是:

    • AbstractClientHttpRequestFactoryWrapper 用来装配其他request factory的抽象类。
    • CommonsClientHttpRequestFactory 允许用户配置带有认证和http连接池的httpclient,已废弃,推荐用HttpComponentsClientHttpRequestFactory。
    • HttpComponentsClientHttpRequestFactory 同2.
    • SimpleClientHttpRequestFactory 接口的一个简单实现,可配置proxy,connectTimeout,readTimeout等参数。

    GET

    Spring的RestTemplate提供了许多的支持,这里仅仅列出常用的接口:

       public <T> T getForObject(String url, Class<T> responseType, Object... urlVariables) throws RestClientException 
       public <T> T getForObject(String url, Class<T> responseType, Map<String, ?> urlVariables) throws RestClientException
       public <T> T getForObject(URI url, Class<T> responseType) throws RestClientException

    对于GET请求来说,我一般常用的几种形式如下:

    String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42", "21");

    或者下面这张形式:

       Map<String, String> vars = Collections.singletonMap("hotel", "42");
       String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);

    以及:

    java String message = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80", String.class );

    他这种做法参考了uri-templateshttps://code.google.com/p/uri-templates/)这个项目。

    POST

    Spring的RestTemplate对post的常用接口:

    public <T> T postForObject(String url, Object request, Class<T> responseType, Object... uriVariables)
                throws RestClientException
            public <T> T postForObject(String url, Object request, Class<T> responseType, Map<String, ?> uriVariables)
                throws RestClientException
            public <T> T postForObject(URI url, Object request, Class<T> responseType) throws RestClientException

    我一般常用的方法为:

    MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<String, String>();
    bodyMap.setAll(urlVariables);
    ResponseClass responseClass = restTemplate.postForObject(CAR_CES_URL, bodyMap, ResponseClass.class);

    以及:

      HttpHeaders headers = new HttpHeaders();
            headers.add("X-Auth-Token", "e348bc22-5efa-4299-9142-529f07a18ac9");
    
            MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
            postParameters.add("owner", "11");
            postParameters.add("subdomain", "aoa");
            postParameters.add("comment", "");
    
            HttpEntity<MultiValueMap<String, String>> requestEntity  = new HttpEntity<MultiValueMap<String, String>>(postParameters, headers);
    
            ParseResultVo exchange = null;
            try {
                exchange = restTemplate.postForObject("http://l-dnsutil1.ops.beta.cn6.qunar.com:10085/v1/cnames/tts.piao",  requestEntity, ParseResultVo.class);
                logger.info(exchange.toString());
            } catch (RestClientException e) {
                logger.info("。。。。");
            }

    以及:

      DomainParam domainParam = new DomainParam();
            domainParam.setCustomerId(1);
            //...
    
            logger.info("....");
            restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
            restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
            String responseResult = restTemplate.postForObject(url, domainParam, String.class);   

    其他

    PUT方式:

    restTemplate.put("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80" ,null); 

    DELETE方式

    //delete方法(注意:delete方法没有返回值,说明,id=0这个参数在服务器端可以不定义该参数,直接使用request获取) 
    // restTemplate.delete("http://localhost:8080/yongbarservice/appstore/appgoods/deleteranking?id=0"); 

    参考资料:

  • 相关阅读:
    SQL语句:Mac 下 处理myql 不能远程登录和本地登录问题
    react中触发事件实现路由跳转
    Sass代码重用----Sass继承
    react实现点击选中的li高亮
    React中通过状态控制元素显示隐藏的三种方法
    React 页面加载后自动执行onClick事件
    formdata实现图片上传
    formdata实现图片上传
    适用于Ant design的省市区联动JSON数据
    react.js
  • 原文地址:https://www.cnblogs.com/m3Lee/p/4138152.html
Copyright © 2011-2022 走看看