LoginSignup
0
0

More than 5 years have passed since last update.

restTemplate template

Posted at
        // 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);

参考

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0