RestTemplate (java) 学习

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

	还有比较常用的httpClient,但是编码复杂,所以学习下即可,建议掌握restTemplate

一:编写配置 RestTemplateConfig

@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

二:学习示例

启动postman 请求 /test/1 传入不同参数,以查看不同方式效果
import cn.study.algorithm.enty.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("test")
public class RestTemplateController {

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("1")
    public String test(@RequestParam("id")String id){

        // post 请求 测试参数
        User user=new User();
        user.setUserName("test1");
        user.setPassword("123");
        user.setAge(22);

        // post请求
        if(id.equals("2")){

            // restTemplate.postForObject() 响应为 指定类型
            /*String url="http://localhost:8080/test/2";
            User forObject = restTemplate.postForObject(url,user,User.class);
            return "1 restTemolate->result:"+forObject;*/

            // restTemplate.postForEntity() 响应为 ResponseEntity
            String url="http://localhost:8080/test/2";
            Map<String,String> map=new HashMap<>();
            map.put("userName","zhangsan");
            map.put("password","123");
            map.put("age","23");
            ResponseEntity<User> userResponseEntity = restTemplate.postForEntity(url, user, User.class);
            return "1 restTemolate->result:"+userResponseEntity;

            // get请求
        }else if(id.equals("3")){
            // restTemplate.getForObject() 响应为 指定类型
            /*String url="http://localhost:8080/test/3?username={1}&age={2}";
            User forObject = restTemplate.getForObject(url, User.class,"test3",5);
            return "1 restTemolate->result:"+forObject;*/

            // restTemplate.getForEntity() 响应为 ResponseEntity
            String url="http://localhost:8080/test/3?username={1}&age={2}";
            ResponseEntity<User> haha = restTemplate.getForEntity(url, User.class, "haha", 3);
            return "1 restTemolate->result:"+haha;

            // 通用 restTemplate.exchange()
        }else if(id.equals("4")){
            String url="http://localhost:8080/test/2";
            HttpEntity<User> httpEntity=new HttpEntity<>(user);
            ResponseEntity<User> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, User.class);
            return "exchange->"+exchange;
        }else{
            RestTemplate restTemplate=new RestTemplate();
            ResponseEntity<String> forEntity = restTemplate.getForEntity("https://www.baidu.com/", String.class);
            System.out.println(forEntity);
            return forEntity.toString();
        }
    }

    // post 请求测试
    @PostMapping("2")
    public User test2(@RequestBody User user){
        System.out.println("我是接口2 收到信息为-》"+user);
        user.setUserName("test2");
        return user;
    }

    // get 请求测试
    @GetMapping("3")
    public User test3(@RequestParam("username")String username,@RequestParam("age")Integer age){
        System.out.println("我是接口3 收到信息为-》"+username);
        User user=new User();
        user.setUserName(username);
        user.setAge(age);
        return user;
    }
}

User.enty

@Data
public class User {
    private String userName;
    private String password;
    private Integer age;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值