我们一般在项目中会遇到某个需求和上一个项目的需求一模一样,那我们就会直接去访问之前项目的接口,不会在另写,或者说调用3方接口。这就用到RestTemplate ,RestTemplate简单的理解就是:简化了发起 HTTP 请求以及处理响应的过程。
自己在本地搭建了两个项目,通过B项目去访问A项目中的接口并处理返回数据;还是老样子,直接看代码。
我首先搭建了两个项目分别是spring-boot和spring-boot-restTemplate;我把spring-boot项目看作三方服务,
然后我在spring-boot项目里面写了一个测试接口来供spring-boot-restTemplate项目调用;因为我们说了是做json的格式传递参数嘛,所以我用了@requestBody,请看下面的代码
package com.steven.controller.appController.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.steven.entity.User;
import com.steven.entityVo.param.Param;
import com.steven.service.UserService;
import com.steven.utils.json.Json;
import com.steven.utils.result.Result;
@Controller
@RequestMapping(value="/test")
public class Test {
@Autowired
private UserService userService;
/**
* 查询用户详细信息
* @param response
* @param userId
*/
@RequestMapping(value="/findOne", method=RequestMethod.POST)
public void findOne(HttpServletRequest request, HttpServletResponse response, @RequestBody Param param) throws Exception{
Integer userId = param.getUserId();
User user = userService.findOne(userId);
if(user == null){
Json.toJson(new Result(false,1,"该用户不存在",null), response);
return;
}
Json.toJson(new Result(true, 0, "获取用户信息成功", user), response);
}
}
接下来就是重点了,主要讲RestTemplate了,在我们项目中引用RestTemplate 需要注册RestTemplate Bean 也就是说我们要写一个配置文件来配置它。这个也很简单,网上资料一大堆。
package com.steven.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.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(10000);//单位为ms
factory.setConnectTimeout(10000);//单位为ms
return factory;
}
}
再然后就是我们的测试接口了,下面代码主要重点关注一下HttpEntuty对象,它其实就是传参的重点。
package com.steven.controller.appController.test;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.steven.entity.User;
import com.steven.utils.json.Json;
import com.steven.utils.result.Result;
import com.steven.utils.string.StringUtil;
@Controller
@RequestMapping(value="/test")
public class Test {
//调用spring里面RestTemplate需要配置bean
@Autowired
private RestTemplate restTemplate;
/**
* post请求远程调用
* @param response
* @param userId
* @throws Exception
*/
@RequestMapping(value="/doPost", method=RequestMethod.POST)
public void doPost(HttpServletResponse response, Integer userId) throws Exception{
String url="http://127.0.0.1/test/findOne";
HttpHeaders headers = new HttpHeaders();
//定义请求参数类型,这里用json所以是MediaType.APPLICATION_JSON
headers.setContentType(MediaType.APPLICATION_JSON);
//RestTemplate带参传的时候要用HttpEntity>对象传递
Map map = new HashMap();
map.put("userId", userId);
HttpEntity
最后看下测试结果测试成功。