需要的包 ,除了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与Spring RestTemplate的更多相关文章

  1. Spring RestTemplate: 比httpClient更优雅的Restful URL访问, java HttpPost with header

    { "Author": "tomcat and jerry", "url":"http://www.cnblogs.com/tom ...

  2. Spring RestTemplate介绍

    http://www.cnblogs.com/rollenholt/p/3894117.html RestTemplate 这篇文章打算介绍一下Spring的RestTemplate.我这边以前设计到 ...

  3. How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查)

    How to disable SSL certificate checking with Spring RestTemplate?(使用resttemplate访问https时禁用证书检查) **** ...

  4. Spring RestTemplate 小结

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

  5. 使用HttpClient4来构建Spring RestTemplate

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

  6. Spring RestTemplate 专题

    相同的参数(接口的入参json打印在日志了)在PostMan中返回预期的数据,但使用RestTemplate时去提示信息错误(参数中汉字).这种情况,搞得怀疑对RestTemplate的理解了使用Re ...

  7. How to Send an HTTP Header With Every Request With Spring RestTemplate

    In Know Which Apps Are Hitting Your Web Service, I showed how to write a servlet filter that enforce ...

  8. Spring RestTemplate详解

    Spring RestTemplate详解   1.什么是REST? REST(RepresentationalState Transfer)是Roy Fielding 提出的一个描述互联系统架构风格 ...

  9. Spring RestTemplate中几种常见的请求方式GET请求 POST请求 PUT请求 DELETE请求

    Spring RestTemplate中几种常见的请求方式 原文地址: https://blog.csdn.net/u012702547/article/details/77917939   版权声明 ...

随机推荐

  1. javascript高级语法

    一.JavaScript对象 1.js对象简介 1.1 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创建对象的方法定义变量,在JavaScrip ...

  2. Django——admin源码分析

    在Django中,如果我们新建一个项目,只要在admin.py文件中注册,就可以对其相应的文件进行增删改查操作. 而我们在路由系统中只看到了一条信息:url(r'^admin/', admin.sit ...

  3. jquery的autocomplete在firefox下不支持中文输入法的bug

    Query.Autocomplete 是jquery的流行插件,能够很好的实现输入框的自动完成(autocomplete).建议提示(input suggest)功能,支持ajax数据加载. 但唯一遗 ...

  4. 每天一个Linux命令(45)lsof命令

        lsof命令用于查看你进程打开的文件,端口(TCP.UDP),找回/恢复删除的文件,打开文件的进程.     (1)用法:     用法:  lsof  [参数]  [文件]     (2)功 ...

  5. ggplot2学习总结

  6. 【HackerRank】Sherlock and Array

    Watson gives an array A1,A2...AN to Sherlock. Then he asks him to find if there exists an element in ...

  7. QT 数字图像处理 笔记一

    1.被有符号整数和无符号整数十足的坑了一上午.我在实现图像旋转的时候先把坐标轴中心平移到图像中心:painter.translate(up_x+temp_w,up_y+temp_h);注意这里面各个数 ...

  8. 【Flask】Sqlalchemy lazy

    ### 懒加载:在一对多,或者多对多的时候,如果想要获取多的这一部分的数据的时候,往往能通过一个属性就可以全部获取了.比如有一个作者,想要或者这个作者的所有文章,那么可以通过user.articles ...

  9. 超酷Loading进度条

    在线演示 本地下载

  10. iOS_多线程(一)

    在学习多线程之前首先搞清楚以下几个问题. 并发:在同一时刻,只有一条指令被执行,多条指令进行快速切换执行. 并行:在同一时刻,多个处理器可以处理多条指令 1.什么是进程?      一个运行的程序就是 ...