SpringBoot使用RestTemplate 跨项目返回对象

SpringBoot使用RestTemplate 跨项目返回对象

在boot项目A中,使用resttemplate访问另一个项目B的方法,返回对象,非返回json.

 

  1. 在A启动类注入bean
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
  1. 写B项目的测试方法,用来提供接口给A查询使用
@Controller
public class indexController {
@ResponseBody
@RequestMapping(“/getUser1”)
public Object getUser1() {
User user = new User();
user.setId(1);
user.setAdds(“中国上海”);
user.setName(“无参数”);
System.out.println(“无参数:” + user.toString());
return user;
}
@ResponseBody
@RequestMapping(“/getUserByName/{name}”)
public User getUser2(@PathVariable String name) {
User user = new User();
user.setId(1);
user.setAdds(“中国上海”);
user.setName(name);
System.out.println(“传入参数:” + user.toString());
return user;
}
@ResponseBody
@RequestMapping(value = “/getUserByUser”, method = RequestMethod.POST)
public User getUser3(@RequestBody User user) {
System.out.println(“传入实体:” + user.toString());
return user;
}

 

  1. A项目访问B项目的具体方法
@RequestMapping(“/”)
@ResponseBody
public void getUser() {
//无需传入参数,直接获取User对象,2种方法
User userA1 = restTemplate.getForObject(“http://localhost:8081/getUser1”, User.class, “”);
User userA2 = RestTemplateUtils.getForEntity(restTemplate, “http://localhost:8081/getUser1”, new ParameterizedTypeReference<User>() {
}, “”).getBody();
// 传入一个属性/参数,返回一个对象,2种方法
User userB1 = restTemplate.postForEntity(“http://localhost:8081/getUserByName/{name}”, String.class, User.class, “wll”).getBody();
User userB2 = RestTemplateUtils.getForEntity(restTemplate, “http://localhost:8081/getUserByName/{name}”, new ParameterizedTypeReference<User>() {
}, “wll”).getBody();
//传入一个对象 一种方法
User user = new User();
user.setId(1);
user.setAdds(“中国上海”);
user.setName(“小明”);
User user3 = RestTemplateUtils.postForEntity(restTemplate, “http://localhost:8081/getUserByUser”, user, new ParameterizedTypeReference<User>() {
}, user).getBody();

}

 

4.其中使用到的一个工具类 RestTemplateUtils ,自己封装的

 

public class RestTemplateUtils {
private RestTemplateUtils() {
}
// GET
public static <T> T getForNullableObject(RestTemplate restTemplate, String url, Class<T> responseType, Object… urlVariables) {
try {
return restTemplate.getForObject(url, responseType, urlVariables);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
return null; }
throw e;
}
}
public static <T> T getForObject(RestTemplate restTemplate, String url, ParameterizedTypeReference<T> responseType, Object… urlVariables) {
return getForEntity(restTemplate, url, responseType, urlVariables).getBody();
}
public static <T> ResponseEntity<T> getForEntity(RestTemplate restTemplate, String url, ParameterizedTypeReference<T> responseType, Object… urlVariables) {
return restTemplate.exchange(url, HttpMethod.GET, null, responseType, urlVariables);
}
public static <T> ResponseEntity<T> getForEntity(RestTemplate restTemplate, URI uri, ParameterizedTypeReference<T> responseType) {
return restTemplate.exchange(uri, HttpMethod.GET, null, responseType);
}
// POST
public static <T> T postForObject(RestTemplate restTemplate, String url, Object request, MediaType contentType, Class<T> responseType, Object… urlVariables) {
return postForEntity(restTemplate, url, request, contentType, responseType, urlVariables).getBody();
}
public static <T> ResponseEntity<T> postForEntity(RestTemplate restTemplate, String url, Object request, MediaType contentType, Class<T> responseType, Object… urlVariables) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(contentType);
return restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(request, headers), responseType, urlVariables);
}
public static <T> T postForObject(RestTemplate restTemplate, String url, Object request, ParameterizedTypeReference<T> responseType, Object… urlVariables) {
return postForEntity(restTemplate, url, request, responseType, urlVariables).getBody();
}
public static <T> ResponseEntity<T> postForEntity(RestTemplate restTemplate, String url, Object request, ParameterizedTypeReference<T> responseType, Object… urlVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(request);
return restTemplate.exchange(url, HttpMethod.POST, requestEntity, responseType, urlVariables);
}
// PUT
public static ResponseEntity<Void> putForEntity(RestTemplate restTemplate, String url, Object request, Object… urlVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(request);
return restTemplate.exchange(url, HttpMethod.PUT, requestEntity, Void.class, urlVariables);
}
// PATCH
public static <T> T patchForObject(RestTemplate restTemplate, String url, Object request, Class<T> responseType, Object… urlVariables) {
return patchForEntity(restTemplate, url, request, responseType, urlVariables).getBody();
}
public static ResponseEntity<Void> patchForEntity(RestTemplate restTemplate, String url, Object request, Object… urlVariables) {
return patchForEntity(restTemplate, url, request, Void.class, urlVariables);
}
public static <T> ResponseEntity<T> patchForEntity(RestTemplate restTemplate, String url, Object request, Class<T> responseType, Object… urlVariables) {
HttpEntity<?> requestEntity = new HttpEntity<>(request);
return restTemplate.exchange(url, HttpMethod.PATCH, requestEntity, responseType, urlVariables);
}
// DELETE
public static ResponseEntity<?> delete(RestTemplate restTemplate, String url, Object request, Object… urlVariables) { HttpEntity<?> requestEntity = new HttpEntity<>(request);
return restTemplate.exchange(url, HttpMethod.DELETE, requestEntity, Void.class, urlVariables);
}

点击下载工具类 RestTemplateUtils

本站的文章多是老王开发工作中问题的记录,一个字一个字敲的,切实可行,可以分享,需要留个原文链接,至少也意思意思吧!
vsalw技术博客! » SpringBoot使用RestTemplate 跨项目返回对象

每个人都是以自己独特的方式体味生活,或许别人不理解,但自己知道:其中的酸甜苦辣就叫做幸福!

认同! 瞎扯淡!