需要的包 ,除了Spring的基础包外还用到json的包,这里的数据传输使用json格式  
客户端和服务端都用到一下的包
 <!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.</version>
</dependency>
一、服务器端代码如下,主要是提供两个接口

@Controller
@RequestMapping("/rest")
public class RestController {   //http://localhost:8080/Springmvc/rest/getPerson
@RequestMapping(value="/getPerson",method=RequestMethod.GET)
@ResponseBody
public Person getPerson(Integer id){
System.out.println("getPerson..."+id);
Person p = new Person("tom", );
return p;
}   //http://localhost:8080/Springmvc/rest/postPerson
@ResponseBody
@RequestMapping(value="/postPerson",method=RequestMethod.POST)
public Person postPerson(@RequestBody Person p){
//@RequestBody用于接收传来的对象,不加的话会接收不到数据
System.out.println("call postPerson :"+p);
return p;
}

二、客户端代码:

1. HttpClient风格的

package com.mvc.rest;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients; import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mvc.entity.Person; public class MyClient { /**
* 需要apache的http包和json包
* */
public static void main(String[] args) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException { //HttpClient是Apache的
HttpClient client = HttpClients.createDefault();
HttpGet request = new HttpGet("http://localhost:8080/Springmvc/rest/getPerson?id=1");
request.setHeader("Accept", "application/json");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity(); //利用json的ObjectMapper把请求到的数据转化为对象
ObjectMapper mapper = new ObjectMapper();
Person p =mapper.readValue(entity.getContent(), Person.class);
System.out.println(p);
} }

2.RestTemplate 风格的

import org.springframework.web.client.RestTemplate;

  
Spring提供的Rest风格的RestTemplate比Apache的HttpClient操作更方便   @Bean(name="restTemplate")
public RestTemplate restTemp(){ return new RestTemplate();
}
  @Autowired
private RestTemplate re; @RequestMapping("/testRestTemplate")
public void get(){ //Person p = re.getForObject("http://localhost:8080/Springmvc/rest/getPerson", Person.class);
Person p = re.postForObject("http://localhost:8080/Springmvc/rest/postPerson", new Person("hello",),Person.class);
//re.put(url, request, urlVariables);
//re.delete(url, urlVariables); System.out.println("call testRestTemplate :"+p);
}

HttpClient的替代者 - RestTemplate的更多相关文章

  1. 使用Httpclient来替代客户端的jsonp跨域解决方案

    最近接手一个项目,新项目需要调用老项目的接口,但是老项目和新项目不再同一个域名下,所以必须进行跨域调用了,但是老项目又不能进行任何修改,所以jsonp也无法解决了,于是想到了使用了Httpclient ...

  2. httpClient和RestTemplate的使用

    1.httpClient的使用 <dependency> <groupId>org.apache.httpcomponents</groupId> <arti ...

  3. SpringBoot系列: RestTemplate 快速入门

    ====================================相关的文章====================================SpringBoot系列: 与Spring R ...

  4. Spring RestTemplate 小结

    关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...

  5. 使用HttpClient4来构建Spring RestTemplate

    Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...

  6. RestTemplate请求https忽略证书认证

    RestTemplate是Spring提供的用于访问Rest服务的客户端,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率.RestTemplate 默认使用J2SE提供的方式( ...

  7. Jsonp方式和httpclient方式有什么区别?

    jsonp基于js,解决跨域问题,本质发起ajax情求但是Jsonp只支持get请求. 它不安全,它先解析js,然后发起ajax请求,然后获取到返回值,通过浏览器返回,最后解析. JQuery和Spr ...

  8. 使用Spring的RestTemplate进行接口调用

    引自:http://www.zimug.com/ 1.常见的http服务的通信方式 经常使用的方式有HttpClient.OkHttp.RestTemplate.其中RestTemplate是一种更优 ...

  9. Spring web之restTemplate超时问题处理

    问题 项目中有个远程服务因为某些原因会访问不通,于是就在调用的那一步挂起无法结束了. 查看代码 代码大概如下 CloseableHttpClient closeableHttpClient = Htt ...

随机推荐

  1. ABP文档 - Hangfire 集成

    文档目录 本节内容: 简介 集成 Hangfire 面板授权 简介 Hangfire是一个综合的后台作业管理器,可以在ABP里集成它替代默认的后台作业管理器,你可以为Hangfire使用相同的后台作业 ...

  2. Git Bash的一些命令和配置

    查看git版本号: git --version 如果是第一次使用Git,你需要设置署名和邮箱: $ git config --global user.name "用户名" $ gi ...

  3. 2017-1-5 天气雨 React 学习笔记

    官方example 中basic-click-counter <script type="text/babel"> var Counter = React.create ...

  4. WPF 微信 MVVM

    公司的同事离职了,接下来的日子可能会忙碌,能完善DEMO的时间也会少了,因此,把做的简易DEMO整体先记录一下,等后续不断的完善. 参考两位大神的日志:WEB版微信协议部分功能分析.[完全开源]微信客 ...

  5. 预览github里面的网页或dome

    1.问题所在: 之前把项目提交到github都可以在路径前面加上http://htmlpreview.github.io/?来预览demo,最近发现这种方式预览的时候加载不出来css,js(原因不详) ...

  6. 小兔Java教程 - 三分钟学会Java文件上传

    今天群里正好有人问起了Java文件上传的事情,本来这是Java里面的知识点,而我目前最主要的精力还是放在了JS的部分.不过反正也不麻烦,我就专门开一贴来聊聊Java文件上传的基本实现方法吧. 话不多说 ...

  7. MSYS2——Windows平台下模拟linux环境的搭建

    最近从MSYS1.0迁移到了MSYS2.0,简单讲,MSYS2.0功能更强大,其环境模拟更加符合linux.虽然本身来自cygwin,但其集成了pacman软件管理工具,很有linux范,并且可以直接 ...

  8. 免费道路 bzoj 3624

    免费道路(1s 128MB)roads [输入样例] 5 7 21 3 04 5 13 2 05 3 14 3 01 2 14 2 1 [输出样例] 3 2 04 3 05 3 11 2 1 题解: ...

  9. Atitit.如何建立研发体系

    Atitit.如何建立研发体系 组织,流程,prj..Mana  oppm 发管理是一个完整的管理体系,从结构上来讲,它主要由四个方面的内容构架而成:组织结构与岗位设置 管理流程与工作流程..项目及管 ...

  10. MongoDB学习笔记四—增删改文档下

    $slice 如果希望数组的最大长度是固定的,那么可以将 $slice 和 $push 组合在一起使用,就可以保证数组不会超出设定好的最大长度.$slice 的值必须是负整数. 假设$slice的值为 ...