RestTemplate
1.我用RestTemplate请求时 我把他注入到容器里 这样可以 什么用什么时候拿
2.也可以new出来 不过我不喜欢 所以就没有用new的
下面我自己的方法 先注入到容器 在 xxx-service.xml 里加上
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate" ></bean>
然后用的时候
@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); HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>( httpHeaders); ResponseEntity<String> 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 MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>(); requestBody.add("xxx", "1"); //HttpEntity HttpEntity<MultiValueMap> requestEntity = new HttpEntity<MultiValueMap>(requestBody, requestHeaders); //post ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://xxx", requestEntity, String.class); System.out.println(responseEntity.getBody());