码迷,mamicode.com
首页 > Web开发 > 详细

HttpClient的替代者 - RestTemplate

时间:2017-01-07 20:52:59      阅读:4564      评论:0      收藏:0      [点我收藏+]

标签:ica   app   cal   end   autowire   auto   request   接收   风格   

需要的包 ,除了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.1</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.1</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.1</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", 12);
        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",34),Person.class);
        //re.put(url, request, urlVariables);
        //re.delete(url, urlVariables);
        
        System.out.println("call testRestTemplate  :"+p);
    }

 

HttpClient的替代者 - RestTemplate

标签:ica   app   cal   end   autowire   auto   request   接收   风格   

原文地址:http://www.cnblogs.com/wwzyy/p/6260176.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!