需要的包 ,除了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. LVM逻辑卷管理器

    LVM概述 通过使用Linux的逻辑卷管理器(Logical Volume Manager, LVM),用户可以在系统运行时动态调整文件系统的大小,把数据从一块硬盘重定位到另一块硬盘,也可以提高I/O ...

  2. equal?, == and eql?, ===,

    1.BasicObject中定义了 == 和equal?这两个方法,两个方法等价,用来比较两个对象是否是同一个对象,是的话结果就为true. 既然两者相同,为何要定义两个呢?只是为了再命名一个别名吗? ...

  3. 【转】Python的hasattr() getattr() setattr() 函数使用方法详解

    Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回BOOL值 ...

  4. 【转】Linux下查看进程打开的文件句柄数

    ---查看系统默认的最大文件句柄数,系统默认是1024 # ulimit -n 1024 ----查看当前进程打开了多少句柄数 # lsof -n|awk '{print $2}'|sort|uniq ...

  5. iOS git 托管代码 常用几个操作

    学习 git 切换分支 1  从远程下载一个分支develop(本地没有的) (1) git fetch origin develop (2) git checkout develop (默认 分支切 ...

  6. JAVA 文件转字节数组转字符串

    public static void main(String[] args) throws IOException { byte[] bytes = FileUtils.readFileToByteA ...

  7. VSCode eslint校验 tab改为2个空格

    修改:.eslintrc.json

  8. java图形验证码

    用java实现验证码的生成,以下代码是一个controller,可以直接使用 package org.jxnd.tongxuelu.controller; import java.awt.Color; ...

  9. scope 作用域

    每当一个指令被创建的时候,都会面临一个选择:继承父作用域,还是创建一个自己的作用域.Angular为指令的scope参数提供了三种选择,分别是: false(继承), true(不继承), {},默认 ...

  10. PostgresSQL数据库安装及操作

    PostgreSQL介绍 PostgreSQL是一个功能强大的开源对象关系数据库管理系统(ORDBMS). 用于安全地存储数据; 支持最佳做法,并允许在处理请求时检索它们. PostgreSQL(也称 ...