RestTemplate发送请求

RestTemplate发送请求_第1张图片

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

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 params = new LinkedMultiValueMap();
        params.add("username", "用户名");
        params.add("password", "123456");
        HttpEntity> requestEntity = new HttpEntity>(params, headers);
        ResponseEntity 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 params = new LinkedMultiValueMap();
        params.add("username", "用户名");
        params.add("password", "123456");
        ResponseEntity 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 params = new LinkedMultiValueMap();
        params.add("username", "用户名");
        params.add("password", "123456");
        HttpEntity> requestEntity = new HttpEntity>(params, headers);
        ResponseEntity response = client.postForEntity(url, requestEntity, String.class);
    }


    /**
     * Form
     */
    public void testFour() {
        String url = "http://localhost/app/login";
        RestTemplate client = new RestTemplate();
        Map map = new HashMap();
        map.put("username", "用户名");
        map.put("password", "123456");
        ResponseEntity 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 params = new HashMap();
        params.put("username", "用户名");
        params.put("password", "123456");
        String value = mapper.writeValueAsString(params);
        HttpEntity requestEntity = new HttpEntity(value, headers);
        ResponseEntity 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 varParams = new HashMap();
        varParams.put("path", "login");
        //一定要设置header
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        ObjectMapper mapper = new ObjectMapper();
        //请求体中的参数
        Map params = new HashMap();
        params.put("username", "用户名");
        params.put("password", "123456");
        String value = mapper.writeValueAsString(params);
        HttpEntity requestEntity = new HttpEntity(value, headers);
        ResponseEntity 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 param = new LinkedMultiValueMap<>();
        param.add("file", resource);
        param.add("name", "abc");
        HttpEntity> httpEntity = new HttpEntity>(param);
        ResponseEntity responseEntity = client.exchange(url, HttpMethod.POST, httpEntity, String.class);
        System.out.println(responseEntity.getBody());
    }
}

 

 

 

 

 

你可能感兴趣的:(tool)