008 SpringBoot-RestTemplate使用

HTTP调用,是一种非常常见的调用技术,业界主流的技术如:Apache HttpClientJDK自带的.net包下的HttpURLConnection类。

RestTemplate,它是Spring自带的非常好用的HTTP调用辅助类!

创建一个IndexController,分别实现一些简单的API接口,然后使用RestTemplate去进行Http调用!

package com.bfxy.springboot.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bfxy.springboot.entity.User;

@RestController
public class IndexController {
        
    private static Logger LOGGER = LoggerFactory.getLogger(IndexController.class);  
    
    @RequestMapping(value = "/get", produces={"application/json;charset=UTF-8"}, method={RequestMethod.GET})
    public User getJson(User user){
        System.err.println("---------===== get ====---------");
        LOGGER.info("id : " + user.getId() + ", name : " + user.getName());
        return user;
    }
    
    @RequestMapping(value = "/post", produces={"application/json;charset=UTF-8"}, consumes = {"application/json;charset=UTF-8"}, method={RequestMethod.POST})
    public User postJson(@RequestBody User user){
        System.err.println("---------===== post ====---------");
        LOGGER.info("id : " + user.getId() + ", name : " + user.getName());
        return user;
    }
    
    
    @RequestMapping(value = "/exchange", produces={"application/json;charset=UTF-8"}, consumes = {"application/json;charset=UTF-8"}, method={RequestMethod.POST})
    public void exchange(@RequestHeader("token")String token, @RequestBody User user){
        System.err.println("exchange 4 post token: " + token + ", user: " + user.getName());
    }    
}
 

package com.bfxy.springboot.util;

import java.net.URI;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class RestTemplateTest {

    
    private static RestTemplate restTemplate = new RestTemplate();
    
    public static void main(String[] args) throws Exception {
        
        /** 
        //get:    
        Map json1 = restTemplate.getForObject("http://localhost:8001/index/get?id=3&name=\"张三\"", Map.class);
        System.err.println("get data: " + json1);
        
        
        //post: 
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", "1003");
        params.put("name", "张13");
        Map<String, String> json2 = restTemplate.postForObject("http://localhost:8001/index/post", params, Map.class);
        System.out.println("post data: " + json2);
        */
        
        //exchange
        
        String body = "{\"id\":\"001\", \"name\": \"张三\"}";
        
        HttpHeaders headers = new HttpHeaders();  
        headers.set("token", "bfxy");    
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        
        HttpEntity<String> entity = new HttpEntity<String>(body, headers);

        ResponseEntity<Map> response = restTemplate.exchange(URI.create("http://localhost:8001/index/exchange"),
                HttpMethod.POST, 
                entity, 
                Map.class);
        
        System.err.println("response code: " + response.getStatusCode());
        
      }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值