SpringMVC提高篇(五):RestTemplate的应用 - Go语言中文社区

SpringMVC提高篇(五):RestTemplate的应用


记得刚到公司的时候,一直在使用HTTPClient这个包每次使用还必须到三个包,真是头大,而且写法都是头大。今天看了看Spring4.0 实践没想到 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"); 


参考资料:

             http://www.cnblogs.com/softidea/p/5977375.html
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/chenpeng19910926/article/details/71131548
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-01 22:50:17
  • 阅读 ( 1249 )
  • 分类:Go应用

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢