Spring boot RestTemplate

简介

        传统情况下在java代码里访问restful服务,一般使用ApacheHttpClient。不过此种方法使用起来太过繁琐。spring提供了一种简单便捷的模板类来进行操作,这就是RestTemplate

        RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具。

HTTP请求的方法:

  • GET:通过请求URI得到资源
  • POST:用于添加新的内容
  • PUT:用于修改某个内容,若不存在则添加
  • DELETE:删除某个内容
  • OPTIONS :询问可以执行哪些方法
  • HEAD :类似于GET, 但是不返回body信息,用于检查对象是否存在,以及得到对象的元数据
  • CONNECT :用于代理进行传输,如使用SSL
  • TRACE:用于远程诊断服务器

案例

服务端:

WebController

package com.springbootw.controller;

import com.springbootw.dto.Persion;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class WebController {

    @RequestMapping("/getUser/{id}")
    private Persion getUser(@PathVariable String id){
        Persion p =new Persion();
        p.setUserName(id+"xxx");
        return p;
    }
    @RequestMapping("/postUser")
    private Persion postUser(@RequestBody Persion p){
        p.setUserName(p.getUserName()+"yyy");
        return p;
    }
}
Persion
package com.springbootw.dto;

public class Persion {
    private String userName;
    private String sex ;

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getUserName() {
        return userName;
    }

    public String getSex() {
        return sex;
    }
}

请求端:

RestT
package com.springbootw2.controller;

import com.springbootw2.dto.Persion;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RestT {

    private final RestTemplate t;

    //当bean没有无参构造函数时,spring将自动拿到有参的构造函数进行自动注入
    //RestTemplateBuilder已经存在于IOC容器中
    public RestT(RestTemplateBuilder b){
        this.t = b.build();
    }

    @RequestMapping("/getRest")
    private String getRest(){
        Persion p = t.getForObject("http://localhost:8080/user/getUser/{id}", Persion.class,"99");
        return p.getUserName();
    }

    @RequestMapping("/postRest")
    private String postRest(){
        Persion ps = new Persion();
        ps.setUserName("卢大宝");
        //包含响应的一些信息
        ResponseEntity<Persion> pp = t.postForEntity("http://localhost:8080/user/postUser",ps ,Persion.class);
        Persion p = pp.getBody();
        return p.getUserName();
    }
}

Persion

package com.springbootw2.dto;

public class Persion {
    private String userName;
    private String sex ;


    public String getUserName() {
        return userName;
    }

    public String getSex() {
        return sex;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

application.yml 

server:
  port: 8090

测试

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值