spring使用控制台运行方式
spring.main.web-application-type=none
新老版本的配置有点差异
Maven的modules只是实现了一个顺序编译,一次多个项目一起生成而己
通过parent才能真正实现继承,目录关系不顶事
@SpringBootApplication @EnableDiscoveryClient @Configuration public class DemoConsoleApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(DemoConsoleApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } @Autowired RestTemplate restTemplate; @Override public void run(String... args) throws Exception { System.out.println("command line start runing now...."); HashMap<String,String> pms=new HashMap<>(); pms.put("a","b"); pms.put("c","d"); String ret = restTemplate.execute("http://xx/welcome", HttpMethod.GET, null, new ResponseExtractor<String>() { @Override public String extractData(ClientHttpResponse clientHttpResponse) throws IOException { InputStream aa = clientHttpResponse.getBody(); String str=""; BufferedReader reader=new BufferedReader(new InputStreamReader(aa)); String line=""; do{ line=reader.readLine(); str+=line+" "; }while(line!=null); return str; } },pms); System.out.println(ret.toString()); } }
rest动态请求微服务
下面这种更牛逼,可以使用header/object 传值方式调用
HashMap<String, String> headerMap = JSON.parseObject(headers, HashMap.class); HashMap<String, Object> paramObj = JSON.parseObject(params, HashMap.class); MultiValueMap<String, String> mtHeader=new LinkedMultiValueMap<>(); headerMap.forEach((String k,String v)->{ mtHeader.set(k,v); }); HttpEntity<?> entiry = new HttpEntity<HashMap<String, Object>>(paramObj,mtHeader); ResponseEntity<String> ret = restTemplate.exchange(url, HttpMethod.POST, entiry, String.class); log.info("restTemplate,status={},statuscode={},body={}", ret.getStatusCode(), ret.getStatusCodeValue(), ret.getBody()); HashMap<String, Object> resultVo = JSON.parseObject(ret.getBody(), HashMap.class); //判断是否消费成功 if (!hasSuccess(resultVo)) { flag = false; }