spring boot · 2020-08-31 0

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方法有:

getForObject

postForObject方法有:

getForObject

put方法有:

getForObject

delete方法有:

getForObject

栗子:

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);
    }
}