RestTemplate
1.我用RestTemplate请求时 我把他注入到容器里 这样可以 什么用什么时候拿
2.也可以new出来 不过我不喜欢 所以就没有用new的
下面我自己的方法 先注入到容器 在 xxx-service.xml 里加上
然后用的时候
@Autowired private RestTemplate restTemplate;
get
@Override public String seleteAllSeasonMatch(){ String url = xxx+"Season?seasontype=2"; HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); httpHeaders.add("xxx",DATAVOLLEY_KEY); HttpEntityrequestEntity = new HttpEntity ( httpHeaders); ResponseEntity responseEntity =restTemplate.exchange(url,HttpMethod.GET,requestEntity,String.class); String result = responseEntity.getBody(); System.out.println(result);; return ""; }
post
HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("xxx", "1.0"); //body MultiValueMaprequestBody = new LinkedMultiValueMap<>(); requestBody.add("xxx", "1"); //HttpEntity HttpEntity requestEntity = new HttpEntity (requestBody, requestHeaders); //post ResponseEntity responseEntity = restTemplate.postForEntity("http://xxx", requestEntity, String.class); System.out.println(responseEntity.getBody());