1.使用 RestTemplate,创建RestTemplateConfig
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2.调用http请求
@Autowired
private RestTemplate restTemplate;
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity<String> stringHttpEntity = new HttpEntity<>(JSON.toJSONString(param), httpHeaders);
JSONObject body = restTemplate.postForEntity(readUrl, stringHttpEntity, JSONObject.class).getBody();
WGJsonResult result = null;
if (body != null && (boolean)body.get("success")) {
}