如何使用Spring boot访问RESTful Web Service
1、使用Eclipse创建标准的Maven工程,在该工程中加入如下的依赖:<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency>

2、在步骤1创建的工程中,创建一个spring boot主类:Application.门钙蹲茌java在该类的main方法中使用R髫潋啜缅estTemplate进行RESTful Web Service的访问package fantasy;import java.util.List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.client.RestTemplate;public class Application { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { //使用RestTemplate模板获取RESTful服务 RestTemplate restTemplate = new RestTemplate(); String serverUrl = "http://localhost:8080/fileList"; List<String> restResult = restTemplate.getForObject(serverUrl, List.class); log.info(restResult.toString()); }}

3、启动图片列表查看的Restful web service服务该服务在之前的案例中可以找到对应的构建方法
4、启动RESTful Web Service消费者应用的application主类,进行服务测试1)使用spring boot模式运行application.java2) 可以在控制台输出看到Restful web service服务的返回结果15:41:53.824 [main] INFO fantasy.Application - [http://localhost:8080/files/60M58PIC5gC_1024.jpg]

5、使用RestTemplate进行Post文件上传服务在application.java中使用如下方法提交文件到服务器中: RestTemplate restTemplate = new RestTemplate(); //使用RestTemplate post提交文件 String postUrl = "http://localhost:8080/upload"; MultiValueMap<String,Object> request =new LinkedMultiValueMap<String,Object>(); FileSystemResource file=new FileSystemResource("E:/mm/m1.jpg"); if(file!=null) { request.add("file",file); } ResponseEntity<String> rst = restTemplate.postForEntity(postUrl, request, String.class); log.info("Post File to Server:"+rst);

6、进行文件上传、文件列表查看测试可以看到文件上传的日志和文件列表查看的日志:16:23:56.191 [main] INFO fantasy.Application - Post File to Server:<302 Found,{Set-Cookie=[JSESSIONID=6F0426D8AA4272E3B79C203D32EBB088; Path=/; HttpOnly], Location=[http://localhost:8080/result;jsessionid=6F0426D8AA4272E3B79C203D32EBB088], Content-Language=[zh-CN], Content-Length=[0], Date=[Tue, 09 Jan 2018 08:23:56 GMT]}>16:23:56.248 [main] INFO fantasy.Application - [http://localhost:8080/files/60M58PIC5gC_1024.jpg, http://localhost:8080/files/m1.jpg]

7、消费者服务类Application.java完整代码如下package fantasy;import java.util.讣嘬铮篌List;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.io.FileSystemResource;import org.springframework.http.ResponseEntity;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.client.RestTemplate;public class Application { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { RestTemplate restTemplate = new RestTemplate(); //使用RestTemplate post提交文件 String postUrl = "http://localhost:8080/upload"; MultiValueMap<String,Object> request =new LinkedMultiValueMap<String,Object>(); FileSystemResource file=new FileSystemResource("E:/mm/m1.jpg"); if(file!=null) { request.add("file",file); } ResponseEntity<String> rst = restTemplate.postForEntity(postUrl, request, String.class); log.info("Post File to Server:"+rst); //使用RestTemplate模板获取RESTful服务:查看文件列表 String serverUrl = "http://localhost:8080/fileList"; List<String> restResult = restTemplate.getForObject(serverUrl, List.class); log.info(restResult.toString()); }}