通過SpringBoot-RestTemplate
- delete() 在特定的URL上對資源執行HTTP DELETE操作
 - exchange() 在URL上執行特定的HTTP方法,返回包含對象的ResponseEntity,這個對象是從響應體中映射得到的
 - execute() 在URL上執行特定的HTTP方法,返回一個從響應體映射得到的對象
 - getForEntity() 發送一個HTTP GET請求,返回的ResponseEntity包含了響應體所映射成的對象
 - getForObject() 發送一個HTTP GET請求,返回的請求體將映射為一個對象
 - postForEntity() POST 數據到一個URL,返回包含一個對象的ResponseEntity,這個對象是從響應體中映射得到的
 - postForObject() POST 數據到一個URL,返回根據響應體匹配形成的對象
 - headForHeaders() 發送HTTP HEAD請求,返回包含特定資源URL的HTTP頭
 - optionsForAllow() 發送HTTP OPTIONS請求,返回對特定URL的Allow頭信息
 - postForLocation() POST 數據到一個URL,返回新創建資源的URL
 - put() PUT 資源到特定的URL
 
導入springboot的web包
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>
    <dependencies>
        <!--CloseableHttpClient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!--spring restTemplate-->
        <!-- @ConfigurationProperties annotation processing (metadata for IDEs)
                生成spring-configuration-metadata.json類,需要引入此類-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies> 
        在啟動類同包下創建RestTemplateConfig.java類
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); factory.setReadTimeout(5000); return factory; } }
然后在Service類(RestTemplateToInterface )中注入使用
import com.alibaba.fastjson.JSONObject; import com.swordfall.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class RestTemplateToInterface { @Autowired private RestTemplate restTemplate; /** * 以get方式請求第三方http接口 getForEntity * @param url * @return */ public User doGetWith1(String url){ ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class); User user = responseEntity.getBody(); return user; } /** * 以get方式請求第三方http接口 getForObject * 返回值返回的是響應體,省去了我們再去getBody() * @param url * @return */ public User doGetWith2(String url){ User user = restTemplate.getForObject(url, User.class); return user; } /** * 以post方式請求第三方http接口 postForEntity * @param url * @return */ public String doPostWith1(String url){ User user = new User("小白", 20); ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, user, String.class); String body = responseEntity.getBody(); return body; } /** * 以post方式請求第三方http接口 postForEntity * @param url * @return */ public String doPostWith2(String url){ User user = new User("小白", 20); String body = restTemplate.postForObject(url, user, String.class); return body; } /** * exchange * @return */ public String doExchange(String url, Integer age, String name){ //header參數 HttpHeaders headers = new HttpHeaders(); String token = "asdfaf2322"; headers.add("authorization", token); headers.setContentType(MediaType.APPLICATION_JSON); //放入body中的json參數 JSONObject obj = new JSONObject(); obj.put("age", age); obj.put("name", name); //組裝 HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers); ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class); String body = responseEntity.getBody(); return body; } }
