栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

resttemplate get(resttemplate鐢ㄦ硶)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

resttemplate get(resttemplate鐢ㄦ硶)

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() 响应为 指定类型
            

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

            // get请求
        }else if(id.equals("3")){
            // restTemplate.getForObject() 响应为 指定类型
            

            // restTemplate.getForEntity() 响应为 ResponseEntity
            String url="http://localhost:8080/test/3?username={1}&age={2}";
            ResponseEntity 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 httpEntity=new HttpEntity<>(user);
            ResponseEntity exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, User.class);
            return "exchange->"+exchange;
        }else{
            RestTemplate restTemplate=new RestTemplate();
            ResponseEntity 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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/775612.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号