Java SpringCloud BibbonRestTemplate.java
package com.fcant.userservice.controller;import com.fcant.userservice.bean.Country;import com.fcant.userservice.bean.User;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestClientException;import org.springframework.web.client.RestTemplate;import java.util.ArrayList;import java.util.Collections;import java.util.List;/*** RestTemplateController* <p>* encoding:UTF-8** @author Fcant 10:06 2019/12/9*/@RestController@RequestMapping("/user")public class RestTemplateController {private static final Logger LOGGER = LoggerFactory.getLogger(RestTemplateController.class);private static final List<User> USER_LIST;static {List<User> userList = new ArrayList<>();userList.add(new User(1L, "User 01", "[email protected]", "男", 1L, ""));userList.add(new User(2L, "User 02", "[email protected]", "女", 2L, ""));USER_LIST = Collections.unmodifiableList(userList);}private RestTemplate restTemplate;@Autowiredpublic RestTemplateController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}@GetMapping("/rest-template")public List<User> queryUser(){List<User> userList = new ArrayList<>(USER_LIST);userList.forEach(user -> {Country country = null;try {country = restTemplate.getForObject("http://area-service/country/{countryId}",Country.class,user.getCountryId());user.setCountryName(country.getCountryName());} catch (RestClientException e) {e.printStackTrace();}});return userList;}}
版权声明
本站仅做备份收录,仅供研究与教学参考之用。
读者将信息用于其他用途的,全部法律及连带责任由读者自行承担,本站不承担任何责任。









评论