SpringBoot的RestTemplate发送GET、POST、PUT、DELETE请求

一、maven依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>

<dependencies>
    <!-- web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

二、服务端

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

    @GetMapping("/get")
    public String get(@RequestParam String name, @RequestParam Integer age) {
        return String.format("%s %s", name, age);
    }

    @PostMapping("/post")
    public String post(@RequestBody Map map) {
        return map.toString();
    }

    @PutMapping("/put")
    public String put(@RequestBody Map map) {
        return map.toString();
    }

    @DeleteMapping("/delete")
    public String delete(@RequestParam String name, @RequestParam Integer age) {
        return String.format("%s %s", name, age);
    }
}

三、客户端

1.getForObject、postForObject、put、delete

getForObject方法有:

postForObject方法有:

put方法有:

delete方法有:

栗子:

public class RestTemplate1Test {

    /**
     * restTemplate.getForObject
     * 无请求体
     * 有返回值
     */
    @Test
    public void testGet() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/get?name={name}&age={age}";

        RestTemplate restTemplate = new RestTemplate();
        String resultStr = restTemplate.getForObject(url, String.class, map);

        System.out.println(resultStr);
    }

    /**
     * restTemplate.postForObject
     * 有请求体
     * 有返回值
     */
    @Test
    public void testPost() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/post";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity entity = new HttpEntity(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        String resultStr = restTemplate.postForObject(url, entity, String.class);

        System.out.println(resultStr);
    }

    /**
     * restTemplate.put
     * 有请求体
     * 无返回值
     */
    @Test
    public void testPut() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/put";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity entity = new HttpEntity(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.put(url, entity);
    }

    /**
     * restTemplate.delete
     * 无请求体
     * 无返回值
     */
    @Test
    public void testDelete() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/delete?name={name}&age={age}";

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.delete(url, map);
    }
}

2.exchange方法

栗子:


public class RestTemplate2Test {

    /**
     * HttpMethod.GET
     */
    @Test
    public void testGet() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/get?name={name}&age={age}";

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, null, String.class, map);

        String body = exchange.getBody();
        System.out.println(body);
    }

    /**
     * HttpMethod.POST
     */
    @Test
    public void testPost() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/post";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity entity = new HttpEntity(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

        String body = exchange.getBody();
        System.out.println(body);
    }

    /**
     * HttpMethod.PUT
     */
    @Test
    public void testPut() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/put";

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);

        HttpEntity entity = new HttpEntity(map, headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class);

        String body = exchange.getBody();
        System.out.println(body);
    }

    /**
     * HttpMethod.DELETE
     */
    @Test
    public void testDelete() {
        Map<String, String> map = new HashMap<>();
        map.put("name", "zz");
        map.put("age", "20");

        String url = "http://localhost:8080/rest/delete?name={name}&age={age}";

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.DELETE, null, String.class, map);

        String body = exchange.getBody();
        System.out.println(body);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值