RestTemplate发送请求

145 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

​编辑

具体方法查看源码!!!这里不再列出(只描述用法)

package fun.lovey.down.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Rest请求
 *
 * @Author: Mr_li
 * @CreateDate: 2018-09-03$ 14:46$
 * @Version: 1.0
 */
public class RestTest {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * Form
     */
    public void testOne() {

        String url = "http://localhost/app/login";
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("username", "用户名");
        params.add("password", "123456");
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
        System.out.println(response.getBody());
    }

    /**
     * Form
     */
    public void testTwo() {
        String url = "http://localhost/app/login";
        RestTemplate client = new RestTemplate();
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("username", "用户名");
        params.add("password", "123456");
        ResponseEntity<String> response = client.postForEntity(url, params, String.class);
    }

    /**
     * Form
     */
    public void testThree() {
        String url = "http://localhost/app/login";
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("username", "用户名");
        params.add("password", "123456");
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);
        ResponseEntity<String> response = client.postForEntity(url, requestEntity, String.class);
    }


    /**
     * Form
     */
    public void testFour() {
        String url = "http://localhost/app/login";
        RestTemplate client = new RestTemplate();
        Map<String, String> map = new HashMap<String, String>();
        map.put("username", "用户名");
        map.put("password", "123456");
        ResponseEntity<String> response = client.postForEntity(url, map, String.class);
    }


    /**
     * Payload
     *
     * @throws JsonProcessingException
     */
    public void test3() throws JsonProcessingException {
        //请求地址
        String url = "http://localhost/mirana-ee/app/login";
        RestTemplate client = new RestTemplate();
        //一定要设置header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        ObjectMapper mapper = new ObjectMapper();
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", "用户名");
        params.put("password", "123456");
        String value = mapper.writeValueAsString(params);
        HttpEntity<String> requestEntity = new HttpEntity<String>(value, headers);
        ResponseEntity<String> response = client.postForEntity(url, requestEntity, String.class);
        System.out.println(response.getBody());
    }

    /**
     * 并不是拼接url
     */
    public void test4() throws JsonProcessingException {
        //在地址中加入格式化参数path
        String url = "http://localhost/app/{path}";
        RestTemplate client = new RestTemplate();
        //准备格式化参数
        Map<String, String> varParams = new HashMap<String, String>();
        varParams.put("path", "login");
        //一定要设置header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        ObjectMapper mapper = new ObjectMapper();
        //请求体中的参数
        Map<String, String> params = new HashMap<String, String>();
        params.put("username", "用户名");
        params.put("password", "123456");
        String value = mapper.writeValueAsString(params);
        HttpEntity<String> requestEntity = new HttpEntity<String>(value, headers);
        ResponseEntity<String> response = client.postForEntity(url, requestEntity, String.class, varParams);
    }

    /**
     * FileUpload
     */
    public void test5() {

        String fileLocal = "E://abc.txt";
        String url = "http://localhost/app/upload";
        RestTemplate client = new RestTemplate();
        FileSystemResource resource = new FileSystemResource(new File(fileLocal));
        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("file", resource);
        param.add("name", "abc");
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(param);
        ResponseEntity<String> responseEntity = client.exchange(url, HttpMethod.POST, httpEntity, String.class);
        System.out.println(responseEntity.getBody());
    }
}