spring的RestTemplate
2015-02-12 16:52
399 查看
参考资料:spring-framework-reference:
1.Accessing RESTful services on the Client
2.Bean scopes下的Session scope
使用java来调用RESTful服务,我们一般会通过HttpClient助手类.其中RestTemplate提供了6种主要Http方法(即DELETE,GET,HEAD,OPTIONS,POST,PUT)的每一种相应更高级别的方法,实现最佳实践.
我们可以简单地调用默认无参构造方法来创建RestTemplate实例,这是使用来自于java.net包的标准java类作为底层实现来创建HTTP请求.这个也可以指定ClientHttpRequestFactory的实现来重写.spring提供了使用Apache HttpComponents的实现HttpComponentsClientHttpRequestFactory来创建请求.使用org.apache.http.client.HttpClient的一个实例来配置HttpComponentsClientHttpRequestFactory,它能配置凭据信息或连接池功能.由spring使用jdk自带类实现的org.springframework.http.client.SimpleClientHttpRequestFactory并不具备存储用户会话的功能.
用法:RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
下面是使用RestTemplate的一个简单例子,至于RestTemplate的API就不介绍了.
1.引入httpclient依赖,在pom.xml加入
3.在Controller下使用,当然在单元测试也可以使用.
1.Accessing RESTful services on the Client
2.Bean scopes下的Session scope
使用java来调用RESTful服务,我们一般会通过HttpClient助手类.其中RestTemplate提供了6种主要Http方法(即DELETE,GET,HEAD,OPTIONS,POST,PUT)的每一种相应更高级别的方法,实现最佳实践.
我们可以简单地调用默认无参构造方法来创建RestTemplate实例,这是使用来自于java.net包的标准java类作为底层实现来创建HTTP请求.这个也可以指定ClientHttpRequestFactory的实现来重写.spring提供了使用Apache HttpComponents的实现HttpComponentsClientHttpRequestFactory来创建请求.使用org.apache.http.client.HttpClient的一个实例来配置HttpComponentsClientHttpRequestFactory,它能配置凭据信息或连接池功能.由spring使用jdk自带类实现的org.springframework.http.client.SimpleClientHttpRequestFactory并不具备存储用户会话的功能.
用法:RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
下面是使用RestTemplate的一个简单例子,至于RestTemplate的API就不介绍了.
1.引入httpclient依赖,在pom.xml加入
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.3</version> </dependency>2.在MvcConfig注册RestTemplate Bean.
@Bean @Scope(value = WebApplicationContext.SCOPE_SESSION,proxyMode = ScopedProxyMode.INTERFACES) public RestTemplate restTemplate(){ ClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); return new RestTemplate(clientHttpRequestFactory); }因为这样的模板工具是属于Web层的东西,放在Web配置更合理.再有,对于scope为session的Bean也必须在WebApplicationContext下使用,如果是在常规的spring容器(即非web环境)下使用会抛IllegalStateException,这点spring-framework-reference有提及.至于这里为什么要使用session的Bean,下面会做分析.
3.在Controller下使用,当然在单元测试也可以使用.
@Controller public class DefaultController { @Autowired private RestOperations restOperations; @RequestMapping("/test") public String test(String url){ //get请求 String result=restOperations.getForObject(url, String.class); System.out.println(result); //post请求,注意:参数不是传给uriVariables,uriVariables是用于设置uri参数变量的 final MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("username", "124124"); params.add("age", "28"); //params.add("file", new MockMultipartFile("test.txt", "sfasfsf".getBytes("UTF-8"))); result = restOperations.postForObject(url, params, String.class); //result = restOperations.postForObject(url, new HttpEntity<>(params, null), String.class); return "user/add"; } }
这里注入RestTemplate的代理实例(因为上面代理是通过JDK生成的,所以这里要用接口注入)供Controller的方法使用.这里的意图就是,传入一个url,然后根据这个url发起请求(不传入参数,是为了简单),返回字符串形式的响应. 对于普通的请求参数(不含上传文件),应使用MultiValueMap<String, String>类型,理由如下,先看org.springframework.http.converter.FormHttpMessageConverter#write
public void write(MultiValueMap<String, ?> map, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { if (!isMultipart(map, contentType)) { writeForm((MultiValueMap<String, String>) map, contentType, outputMessage); } else { writeMultipart((MultiValueMap<String, Object>) map, outputMessage); } } private boolean isMultipart(MultiValueMap<String, ?> map, MediaType contentType) { if (contentType != null) { return MediaType.MULTIPART_FORM_DATA.includes(contentType); } for (String name : map.keySet()) { for (Object value : map.get(name)) { if (value != null && !(value instanceof String)) { return true; } } } return false; }
从isMultipart方法可以看出value不为空且全是String类型才认为不是Multipart请求,参数会以key1=value1&key2=value2形式写入请求体,在springMvc的controller方法public String list(String username,Integer age){}是可以正常映射. 对于Multipart请求,请求的另一种形式 --9EGuVcL0OtjH40Pnd-MjN5Cs6xyUM9FPzX Content-Disposition: form-data; name="username" Content-Type: text/plain;charset=ISO-8859-1 Content-Length: 9 124124 --9EGuVcL0OtjH40Pnd-MjN5Cs6xyUM9FPzX Content-Disposition: form-data; name="age" Content-Type: application/json;charset=UTF-8 28 这种传Multipart形式请求(如果有上传文件,不能用get请求传输,并且要指定为这种Multipart,我也郁闷没用restTemplate成功上传过文件),在springMvc不配MultipartResolver,参数是不能正常解析的.虽然可能不会报错.但只要在servletApplicationContext声明以下Bean就可以
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency>
@Bean public MultipartResolver multipartResolver() { CommonsMultipartResolver bean = new CommonsMultipartResolver(); bean.setDefaultEncoding("UTF-8"); return bean; }
有了MultipartResolver之后,上面的params.add("age","28");改为params.add("age",28);完全没问题
4.测试.就拿前文作为服务端测试例子,此例对于要从/user/list获取数据,用户必须有"user_list"的权限,如果用户没登录,没有这权限,就会返回403,禁止访问. a.先模拟用户登录,在浏览器输入:http://localhost:8081/web2/test?url=http://localhost:8080/web1/user/login?id=1 b.然后在用户登录的前提下去获取数据,再输入:http://localhost:8081/web2/test?url=http://localhost:8080/web1/user/list 这测试都很顺利正常,下面分析为什么要用session的Bean(建议看spring-framework-reference的Session scope): 因为对于不同的用户有不同的会话,如果使用singleton的话,那么会造成所有的用户都共用这个RestTemplate,并不希望有一个用户登录了,另一个用户根本没有登录,就可以前一个用户的身份去发请求,再有新登录的用户也会覆盖旧用户登录的信息. 使用了session的restTemplate Bean即可解决此问题.这样使用,它的有效作用范围就是HTTP会话级别,即不同的会话,DefaultController的restOperations会注入不同的代理.(为什么要使用代理,建议去看spring-framework-reference的Scoped beans as dependencies,代理有两种,这里使用基于JDK).
相关文章推荐
- Spring RestTemplate 进行微信公众号请求的代码笔记
- Spring RestTemplate详解
- spring restTemplate 用法
- SpringMVC Mybatis Shiro RestTemplate的实现客户端无状态验证及访问控制【转】
- SpringMVC RestTemplate的几种请求调用
- Spring之RestTemplate介绍
- Spring RestTemplate详解
- Spring Boot中RestTemplate的使用
- Spring-boot RestTemplate 使用 报错问题
- 【web】Spring RestTemplate提交时设置http header请求头
- Spring restTemplate使用
- 通过 Spring RestTemplate 调用带请求体的 Delete 方法(Delete With Request Body)
- spring rest mvc使用RestTemplate调用
- Spring RestTemplate Add Header
- SpringBoot中,RestTemplate中文乱码解决方案
- spring cloud RestTemplate消费者使用Hystrix进行容错和服务降级
- springboot 集成RestTemplate
- Spring中使用RestTemplate调用第三方接口API
- Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header
- Spring RestTemplate出现乱码解决方法