RestTemplate简单使用

RestTemplate

  • 引入Maven包
  • 配置
  • 使用代码模板
  • POST设置Header
  • GET设置Header

引入Maven包

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-webartifactId>
dependency>

配置

package org.brando.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        RestTemplate restTemplate = new RestTemplate(factory);
        //设置返回编码, 不设置返回汉字将会出现乱码.
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("utf-8")));
        return restTemplate;
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);   //毫秒
        factory.setConnectTimeout(5000);//毫秒
        return factory;
    }

}

使用代码模板

package org.brando.controller;

import org.brando.entity.MemberInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;

@RestController
public class MemberInfoController {

    private static final String REST_URL_PREFIX = "http://127.0.0.1:8001/member-center";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/memberInfo/find/{gid}", method = RequestMethod.GET)
    public MemberInfo memberInfoFindByGid(@PathVariable Long gid) {
        MemberInfo memberInfo = restTemplate.getForObject(REST_URL_PREFIX + "/view/memberInfo/find/" + gid, MemberInfo.class);
        return memberInfo;
    }

}


POST设置Header

	// 组装请求参数, 使用 FastJSON 进行 JSON 字符串生成.
	JSONObject json = new JSONObject();
	json.put("username", "admin");
	json.put("password", "admin123");
	// 设置 Header 为 JSON 格式, 并且使用 UTF-8 编码
	HttpHeaders headers = new HttpHeaders();
	MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
	headers.setContentType(type);
	headers.add("Accept", MediaType.APPLICATION_JSON.toString());
	// 创建请求参数对象
	HttpEntity<String> request = new HttpEntity<String>(json.toString(), headers);
	// 请求并返回 String 对象结果, 
	// url 为访问PAI地址, 例如: http://127.0.0.1:8001/member-center/login
	ResponseEntity<String> res = restTemplate.postForEntity(url, request, String.class);
	JSONObject obj = JSONObject.parseObject(res.getBody());
    String token = (String)obj.get("token");
    

GET设置Header

	HttpHeaders headers = new HttpHeaders();
	// 设置登陆授权 token
	headers.add("Authorization", token);
	MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
	headers.setContentType(type);
	headers.add("Accept", MediaType.APPLICATION_JSON.toString());
	// 此处第一个参数为请求参数, 因为是GET请求, 请求参数可以直接组装到 url 所以直接传 null
	HttpEntity<String> request = new HttpEntity<String>(null, headers);
	// 因为 GET 函数没有 设置Header的方法, 所以使用 exchange 函数来实现.
	ResponseEntity<String> res = restTemplate.exchange(url, HttpMethod.GET, request, String.class);
	JSONObject obj = JSONObject.parseObject(res.getBody());

你可能感兴趣的:(Java)