通過JAX-RS Client接口訪問HTTP---------------------JAX-RS學習筆記之一

一、實現代碼

import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Response;
public class MainApp {
    public static void main(String[] args) {

        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://www.baidu.com");
        Invocation.Builder builder=target.request();
        Response response = builder.get();
        System.out.println(response.getStatusInfo().getReasonPhrase());
    }

}

說明,以上代碼是直接使用JAX-RS標準中的Client接口,非常簡潔。

二、需要的pom.xml

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.25.1</version>
    <type>jar</type>
</dependency>

三、相關接口依賴

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章