SpringBoot下的远程调用

注入restTemplate,用它来访问远程对象

package com.pp.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {
    //builder本来就有
	@Autowired
	private RestTemplateBuilder builder;

	@Bean
	public RestTemplate restTemplate() {
		return builder.build();
	}
}

启动另一项目 ,在SpringBoot项目下访问/remote接口

@Autowired
	private RestTemplate restTemplate;
	
	@RequestMapping("/remote")
	@ResponseBody
	public List remote(){
		//8084是tomcat端口号+接口名+不加项目名
		String url="http://localhost:8084/easywork-v3/pp/Student/selectAllx";
		return (List) restTemplate.getForObject(url, Object.class);
	}

 

你可能感兴趣的:(SpringBoot下的远程调用)