// create restTemplate
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
InetSocketAddress address = new InetSocketAddress(proxySettings.getHost(), proxySettings.getPort());
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
factory.setProxy(proxy);
RestTemplate restTemplate = restTemplateBuilder.requestFactory(factory).build();
// set headers
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("User-Agent", "hogeclient");
httpHeaders.add("Content-Type", "application/json; charset=utf-8");
// set body as JSON
ObjectMapper mapper = new ObjectMapper();
ObjectNode objectNode = mapper.createObjectNode();
objectNode.put("key", body);
// set uri
try {
uri = new URI("http://hoge.io/");
} catch (URISyntaxException e) {
e.printStackTrace();
}
// create requestEntity
RequestEntity<?> requestEntity = new RequestEntity<>(httpHeaders, HttpMethod.GET, uri);
// request
ResponseEntity<?> responseEntity = restTemplate.exchange(requestEntity, String.class);
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme