RestTemplate 简单使用(笔记)

get

    public static void get(){
        RestTemplate restTemplate = new RestTemplate();

        //不带参数get
        Notice notice = restTemplate.getForObject("http://localhost:8088/rest/notice",Notice.class);
        System.out.println(notice.toString());

        //带参数get (占位符)
        notice = restTemplate.getForObject("http://localhost:8088/rest/{1}/{2}",Notice.class,"hello",5);
        System.out.println(notice.toString());

        //带参数get (map)
        Map<String,String> map = new HashMap();
        map.put("start1","6");
        map.put("page2","5");

        notice = restTemplate.getForObject("http://localhost:8088/rest/page?start={start1}&page={page2}",Notice.class,map);
        System.out.println(notice.toString());
    }

    /**
     * 只是返回参数和get不一样
     */
    public static void entity(){
        RestTemplate restTemplate = new RestTemplate();

        //不带参数get
        ResponseEntity<Notice> notice = restTemplate.getForEntity("http://localhost:8088/rest/notice",Notice.class);
        System.out.println(notice.getBody().toString());

        //带参数get (占位符)
        notice = restTemplate.getForEntity("http://localhost:8088/rest/{1}/{2}",Notice.class,"hello",5);
        System.out.println(notice.getBody().toString());

        //带参数get (map)
        Map<String,String> map = new HashMap();
        map.put("start1","6");
        map.put("page2","5");

        notice = restTemplate.getForEntity("http://localhost:8088/rest/page?start={start1}&page={page2}",Notice.class,map);
        System.out.println(notice.getBody().toString());
    }

post

    /**
     * post
     */
    public static void post(){
        RestTemplate restTemplate = new RestTemplate();

        MultiValueMap<String, String> requestMap= new LinkedMultiValueMap<String, String>();
        requestMap.add("client_id", "123");
        requestMap.add("app_code", "abc");
        HttpEntity requestEntity = new HttpEntity(requestMap,null);
        String responseAsString = restTemplate.postForObject("http://localhost:8088/rest/post",requestEntity,String.class);
        System.out.println("post1:" + responseAsString);

        //发送json数据
        requestMap= new LinkedMultiValueMap<String, String>();
        requestMap.add("client_id", "123");
        requestMap.add("app_code", "abc");
        // 必须加上header说明
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> jsonEntity = new HttpEntity<String>(new Gson().toJson(requestMap),headers);
        responseAsString = restTemplate.postForObject("http://localhost:8088/rest/post2",jsonEntity,String.class);
        System.out.println("postJson:" + responseAsString);
    }
exchange
    /**
     * 可以指定调用类型
     */
    public static void exchange(){
        RestTemplate restTemplate = new RestTemplate();

        //发送post
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
        params.add("pageNo",1);
        HttpEntity entity = new HttpEntity(params,null);
        Notice testList = restTemplate.exchange("http://localhost:8088/rest/exchange1", HttpMethod.POST, entity, new ParameterizedTypeReference<Notice>() {}).getBody();
        System.out.println(testList.toString());

        Notice notice = restTemplate.exchange("http://localhost:8088/rest/exchange2?pageNo=66", HttpMethod.GET, null,new ParameterizedTypeReference<Notice>() {}).getBody() ;
        System.out.println(notice.toString());
    }

对应controller

@RestController
@RequestMapping("/rest")
public class RestController {

    @GetMapping("/notice")
    public Notice notice(){
        Notice notice = new Notice();
        notice.setMsg("666");
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }

    @GetMapping("/hello/5")
    public Notice hello5(){
        Notice notice = new Notice();
        notice.setMsg("hello5");
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }

    @GetMapping("/page")
    public Notice page(HttpServletRequest request){
        String start = request.getParameter("start");
        String page = request.getParameter("page");

        Notice notice = new Notice();
        notice.setMsg("start:" + start + ", page:" + page);
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }

    @PostMapping("/post")
    public Notice post1(HttpServletRequest request){
        String client_id = request.getParameter("client_id");
        String app_code = request.getParameter("app_code");

        Notice notice = new Notice();
        notice.setMsg("client_id:" + client_id + ", app_code:" + app_code);
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }

    @PostMapping("/post2")
    public Notice post2(@RequestBody String json){
        Notice notice = new Notice();
        notice.setMsg("json:" + json );
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }

    @PostMapping("/exchange1")
    public Notice exchange1(HttpServletRequest request){
        String pageNo = request.getParameter("pageNo");

        Notice notice = new Notice();
        notice.setMsg("post---pageNo:" + pageNo);
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }

    @GetMapping("/exchange2")
    public Notice exchange2(HttpServletRequest request){
        String pageNo = request.getParameter("pageNo");

        Notice notice = new Notice();
        notice.setMsg("get---pageNo:" + pageNo);
        notice.setStatus(1);
        notice.setData(new LinkedList<>());
        return notice;
    }
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值