第一步
@Service
public class HttpClient {
public JSONObject client(String url, HttpMethod method, MultiValueMap<String,String> params){
RestTemplate template = restTemplate();
ResponseEntity<String> response1=template.getForEntity(url,String.class);
return JSONObject.parseObject(response1.getBody());
}
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
messageConverters.set(1,
new StringHttpMessageConverter(StandardCharsets.UTF_8));
return restTemplate;
}
}
第二步 调用
@RequestMapping(value = "/getNumber")
public JSONObject getNumber(){
String url = "https://xxxxxx";
HttpMethod method = HttpMethod.GET;
MultiValueMap<String,String> params = new LinkedMultiValueMap<>();
System.out.println("url"+url);
return httpClient.client(url,method,params);
}
done
|