场景:使用spring的RestTemplate,进行内部服务间调用,解耦服务
1.创建spring的web项目,保证服务在tomcat中正常运行。
2.本例调用rest服务传递一个实体对象,转为json字符串后传递
public class CityInfoModel implements Serializable {
private static final long serialVersionUID = 4443714978780088961L;
/** 城市名称 */
String cityName;
/**城市面积*/
double landArea;
/**人口*/
long population;
/**生产总值*/
double gross ;
/** 行政区划代码 */
String areaNumber;
/** 邮政编码 */
String postalCode;
/** 电话区号 */
String telephoneCode;
/**车牌代码*/
String carCode;
/** 城市描述 */
String cityDescribe;
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public double getLandArea() {
return landArea;
}
public void setLandArea(double landArea) {
this.landArea = landArea;
}
public long getPopulation() {
return population;
}
public void setPopulation(long population) {
this.population = population;
}
public double getGross() {
return gross;
}
public void setGross(double gross) {
this.gross = gross;
}
public String getAreaNumber() {
return areaNumber;
}
public void setAreaNumber(String areaNumber) {
this.areaNumber = areaNumber;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getTelephoneCode() {
return telephoneCode;
}
public void setTelephoneCode(String telephoneCode) {
this.telephoneCode = telephoneCode;
}
public String getCarCode() {
return carCode;
}
public void setCarCode(String carCode) {
this.carCode = carCode;
}
public String getCityDescribe() {
return cityDescribe;
}
public void setCityDescribe(String cityDescribe) {
this.cityDescribe = cityDescribe;
}
@Override
public String toString() {
return "CityModel [cityName=" + cityName
+",landArea=" + landArea
+",population=" + population
+",gross=" + gross
+",areaNumber=" + areaNumber
+",postalCode=" + postalCode
+",telephoneCode=" + telephoneCode
+",carCode=" + carCode
+",cityDescribe=" + cityDescribe
+"]";
}
}
3.使用RestTemplate封装
public class OperateRestTemplateUtils {
private static Logger logger = LoggerFactory.getLogger(OperateRestTemplateUtils.class);
private static RestTemplate restTemplate = new RestTemplate();
public static <T> T postForObject(String url, Object paramMap,Class<T> responseType) throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
httpHeaders.setContentType(type);
httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
String jsonStr = JSON.toJSONString(paramMap);
System.out.println("发送请求参数为:" + jsonStr.toString());
HttpEntity<String> formEntity = new HttpEntity<String>(jsonStr.toString(), httpHeaders);
return restTemplate.postForObject(url, formEntity, responseType);
}
}
4.测试main方法,其中url中 study是项目名称
public class TestRestTemplate {
private static final String url = "http://localhost:8080/study/service/city";
public static void main(String[] args) throws Exception {
CityInfoModel city = new CityInfoModel();
city.setCityName("厦门");
city.setCityDescribe("适合居住的城市.");
System.out.println("测试开始......");
String objJson = OperateRestTemplateUtils.postForObject(url, city,String.class);
System.out.println(objJson);
System.out.println("测试结束......");
}
}
5.服务端controller示例
@Controller
@RequestMapping("/service")
public class CityController {
private static final Logger logger = LoggerFactory.getLogger(CityController.class);
@ResponseBody
@RequestMapping(value = "city" ,method = RequestMethod.POST )
public String cityInfo(@RequestBody CityInfoModel cityModel) {
logger.info("进入CityController");
logger.info(cityModel.toString());
logger.info("退出CityController");
return "城市信息";
}
}
以上,TKS.