RESTTemplate的使用

RESTTemplate的使用

spring官方提供的发送请求的模板

TestController

package indi.yuluo.resttemplate.controller;

import indi.yuluo.resttemplate.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: yuluo
 * @FileName: TestController.java
 * @createTime: 2022/6/17 12:33
 * @Description:
 */

@RestController
public class TestController {

    @GetMapping("/testGet")
    public String get(String name) {

        System.out.println(name);

        return "ok";
    }

    /**
     * post传参常见的有两种
     * 1、application/json
     *      json参数的核心在content-type 是 application/json类型 charset=utf-8
     * 2、form 表单参数
     *      header content-type = application/x-www-form-urlencoded
     * @param user
     * @return
     */
    @PostMapping("/testPost1")
    public String post(@RequestBody User user) {

        System.out.println(user);

        return "ok";
    }

    @PostMapping(value = "/testPost2")
    public String post2(User user) {
        System.out.println(user);

        return "ok";
    }

}

Test测试类

package indi.yuluo.resttemplate;

import indi.yuluo.resttemplate.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;

@SpringBootTest
class ApplicationTests {

    @Test
    void contextLoads() {
        // 在java代码中发送请求
        RestTemplate restTemplate = new RestTemplate();

        // 返回值是一个object  将返回值转转换成为string类型 返回的是html页面代码
        String forObject = restTemplate.getForObject("https://www.baidu.com", String.class);
        System.out.println(forObject);
    }

    /**
     *  用于测试:restTemplate发送get和post请求
     */
    @Test
    public void testGet() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/testGet?name=yuluo";

        String result = restTemplate.getForObject(url, String.class);
        System.out.println(result);
    }

    /**
     *  用于测试:getForEntity
     */
    @Test
    public void testGet2() {
        RestTemplate restTemplate = new RestTemplate();

        String url = "http://localhost:8080/testGet?name=yuluo";

        /*
        http:// ip 协议 (规范,)
        请求头,请求参数 响应头 相应状态码,报文
         */
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);

        System.out.println(responseEntity);
    }

    /**
     *  用于测试: restTemplate post请求
     */
    @Test
    public void testPost1() {
        RestTemplate restTemplate = new RestTemplate();

        User user = new User();
        user.setAge(20);
        user.setName("yuluo");

        String url = "http://localhost:8080/testPost1";

        /*
        发送post请求,是json参数,web中默认使用的是jackson,将对象转换成json字符串
         */
        String result = restTemplate.postForObject(url, user, String.class);

        System.out.println(result);
    }

    /**
     *  用于测试: 发送post表单参数
     */
    @Test
    public void test() {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/testPost2";

        // 设置表单参数
        LinkedMultiValueMap<String, Object> linkedMultiValueMap = new LinkedMultiValueMap<>();
        linkedMultiValueMap.add("name", "yuluo");
        linkedMultiValueMap.add("age", 21);

        String result = restTemplate.postForObject(url, linkedMultiValueMap, String.class);

        System.out.println(result);
    }

}

对象

package indi.yuluo.resttemplate.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author: yuluo
 * @FileName: User.java
 * @createTime: 2022/6/17 12:35
 * @Description:
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private String name;

    private Integer age;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RestTemplateSpring提供的一个用于访问RESTful Web服务的客户端工具类,它封装了HTTP请求和响应的细节,使得开发者可以方便地进行RESTful Web服务的调用。 使用RestTemplate需要进行以下步骤: 1. 创建RestTemplate实例 可以使用以下代码创建一个RestTemplate实例: ``` RestTemplate restTemplate = new RestTemplate(); ``` 2. 发送HTTP请求 可以使用RestTemplate的一些方法发送HTTP请求,例如: ``` // GET请求 String result = restTemplate.getForObject(url, String.class); // POST请求 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); String result = restTemplate.postForObject(url, entity, String.class); ``` 其中,`url`表示请求的URL,`String.class`表示请求返回的数据类型,`requestBody`表示POST请求中的请求体。 3. 处理HTTP响应 RestTemplate提供了一些方法用于处理HTTP响应,例如: ``` // GET请求 ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); HttpStatus statusCode = response.getStatusCode(); String responseBody = response.getBody(); // POST请求 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); HttpEntity<String> entity = new HttpEntity<>(requestBody, headers); ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); HttpStatus statusCode = response.getStatusCode(); String responseBody = response.getBody(); ``` 其中,`ResponseEntity`表示HTTP响应的实体类,包含了响应头、响应状态码和响应体等信息,`statusCode`表示响应状态码,`responseBody`表示响应体。 以上就是使用RestTemplate的基本流程。在实际的开发中,可以根据实际需求选择不同的RestTemplate方法来发送HTTP请求和处理HTTP响应。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值