com.github.hexsmith.spring.boot.rest.client.ApacheHttpClient.java Source code

Java tutorial

Introduction

Here is the source code for com.github.hexsmith.spring.boot.rest.client.ApacheHttpClient.java

Source

/*
 *  Copyright(C) 2016-2018 The hexsmith Authors
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package com.github.hexsmith.spring.boot.rest.client;

import com.github.hexsmith.spring.boot.rest.model.User;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * Apache ApacheHttpClient
 * @author hexsmith
 * @version v1.0
 * @since 2018/7/13 23:23
 */
public class ApacheHttpClient {

    public static void main(String[] args) {
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

        HttpClient httpClient = httpClientBuilder.build();

        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);

        RestTemplate restTemplate = new RestTemplate(factory);

        User user = restTemplate.getForObject("http://localhost:8080/json/user", User.class);

        System.out.println(user);

    }
}