大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 rest接口調(diào)用方法詳解

rest接口調(diào)用方法詳解

更新時(shí)間:2022-09-06 07:29:38 來源:動(dòng)力節(jié)點(diǎn) 瀏覽993次

由于在實(shí)際項(xiàng)目中碰到的restful服務(wù),參數(shù)都以json為準(zhǔn)。這里獲取的接口和傳入的參數(shù)都是json字符串類型。基于發(fā)布的Restful服務(wù),下面總結(jié)幾種常用的調(diào)用方法。

1.Jersey API

package com.restful.client;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import javax.ws.rs.core.MediaType;
/**
 * Created by XuHui on 2017/8/7.
 */
public class JerseyClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        getRandomResource();
        addResource();
        getAllResource();
    }
    public static void getRandomResource() {
        Client client = Client.create();
        WebResource webResource = client.resource(REST_API + "/getRandomResource");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept("application/json").get(ClientResponse.class);
        String str = response.getEntity(String.class);
        System.out.print("getRandomResource result is : " + str + "\n");
    }
    public static void addResource() throws JsonProcessingException {
        Client client = Client.create();
        WebResource webResource = client.resource(REST_API + "/addResource/person");
        ObjectMapper mapper = new ObjectMapper();
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, mapper.writeValueAsString(entity));
        System.out.print("addResource result is : " + response.getEntity(String.class) + "\n");
    }
    public static void getAllResource() {
        Client client = Client.create();
        WebResource webResource = client.resource(REST_API + "/getAllResource");
        ClientResponse response = webResource.type(MediaType.APPLICATION_JSON).accept("application/json").get(ClientResponse.class);
        String str = response.getEntity(String.class);
        System.out.print("getAllResource result is : " + str + "\n");
    }
}

結(jié)果:

getRandomResource result is : {"id":"NO1","name":"Joker","addr":"http:///"}
addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}]

2.HttpURLConnection

package com.restful.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Created by XuHui on 2017/8/7.
 */
public class HttpURLClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        addResource();
        getAllResource();
    }
    public static void addResource() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        URL url = new URL(REST_API + "/addResource/person");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        httpURLConnection.setRequestProperty("Content-Type", "application/json");
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        OutputStream outputStream = httpURLConnection.getOutputStream();
        outputStream.write(mapper.writeValueAsBytes(entity));
        outputStream.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String output;
        System.out.print("addResource result is : ");
        while ((output = reader.readLine()) != null) {
            System.out.print(output);
        }
        System.out.print("\n");
    }
    public static void getAllResource() throws Exception {
        URL url = new URL(REST_API + "/getAllResource");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setRequestProperty("Accept", "application/json");
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
        String output;
        System.out.print("getAllResource result is :");
        while ((output = reader.readLine()) != null) {
            System.out.print(output);
        }
        System.out.print("\n");
    }
}

結(jié)果:

addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is :[{"id":"NO2","name":"Joker","addr":"http://"}]

3.HttpClient

package com.restful.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
/**
 * Created by XuHui on 2017/8/7.
 */
public class RestfulHttpClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        addResource();
        getAllResource();
    }
    public static void addResource() throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        ObjectMapper mapper = new ObjectMapper();
        HttpPost request = new HttpPost(REST_API + "/addResource/person");
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Accept", "application/json");
        StringEntity requestJson = new StringEntity(mapper.writeValueAsString(entity), "utf-8");
        requestJson.setContentType("application/json");
        request.setEntity(requestJson);
        HttpResponse response = httpClient.execute(request);
        String json = EntityUtils.toString(response.getEntity());
        System.out.print("addResource result is : " + json + "\n");
    }
    public static void getAllResource() throws Exception {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet(REST_API + "/getAllResource");
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Accept", "application/json");
        HttpResponse response = httpClient.execute(request);
        String json = EntityUtils.toString(response.getEntity());
        System.out.print("getAllResource result is : " + json + "\n");
    }
}

結(jié)果:

addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}]

maven:

<dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.1.2</version>
</dependency>

4.JAX-RS API

package com.restful.client;
import com.restful.entity.PersonEntity;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
/**
 * Created by XuHui on 2017/7/27.
 */
public class RestfulClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        getRandomResource();
        addResource();
        getAllResource();
    }
    public static void getRandomResource() throws IOException {
        Client client = ClientBuilder.newClient();
        client.property("Content-Type","xml");
        Response response = client.target(REST_API + "/getRandomResource").request().get();
        String str = response.readEntity(String.class);
        System.out.print("getRandomResource result is : " + str + "\n");
    }
    public static void addResource() {
        Client client = ClientBuilder.newClient();
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        Response response = client.target(REST_API + "/addResource/person").request().buildPost(Entity.entity(entity, MediaType.APPLICATION_JSON)).invoke();
        String str  = response.readEntity(String.class);
        System.out.print("addResource result is : " + str + "\n");
    }
    public static void getAllResource() throws IOException {
        Client client = ClientBuilder.newClient();
        client.property("Content-Type","xml");
        Response response = client.target(REST_API + "/getAllResource").request().get();
        String str = response.readEntity(String.class);
        System.out.print("getAllResource result is : " + str + "\n");
    }
}

結(jié)果:

getRandomResource result is : {"id":"NO1","name":"Joker","addr":"http:///"}
addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}]

5.webClient

package com.restful.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.restful.entity.PersonEntity;
import org.apache.cxf.jaxrs.client.WebClient;
import javax.ws.rs.core.Response;
/**
 * Created by XuHui on 2017/8/7.
 */
public class RestfulWebClient {
    private static String REST_API = "http://localhost:8080/jerseyDemo/rest/JerseyService";
    public static void main(String[] args) throws Exception {
        addResource();
        getAllResource();
    }
    public static void addResource() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        WebClient client = WebClient.create(REST_API)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .encoding("UTF-8")
                .acceptEncoding("UTF-8");
        PersonEntity entity = new PersonEntity("NO2", "Joker", "http://");
        Response response = client.path("/addResource/person").post(mapper.writeValueAsString(entity), Response.class);
        String json = response.readEntity(String.class);
        System.out.print("addResource result is : " + json + "\n");
    }
    public static void getAllResource() {
        WebClient client = WebClient.create(REST_API)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .encoding("UTF-8")
                .acceptEncoding("UTF-8");
        Response response = client.path("/getAllResource").get();
        String json = response.readEntity(String.class);
        System.out.print("getAllResource result is : " + json + "\n");
    }
}

結(jié)果:

addResource result is : {"id":"NO2","name":"Joker","addr":"http://"}
getAllResource result is : [{"id":"NO2","name":"Joker","addr":"http://"}

maven:

<dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-bundle-jaxrs</artifactId>
      <version>2.7.0</version>
</dependency>

注:該jar包引入和jersey包引入有沖突,不能在一個(gè)工程中同時(shí)引用。

提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-04-24 搶座中
  • 全國(guó)校區(qū) 2025-05-15 搶座中
  • 全國(guó)校區(qū) 2025-06-05 搶座中
  • 全國(guó)校區(qū) 2025-06-26 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 久久这里只有精品免费的 | 国产午夜免费 | 中文字幕 二区 三区 搜查官 | 久久亚洲免费视频 | 国产精品自拍一区 | 久久久久亚洲香蕉网 | 日本精品中文字幕在线不卡 | 天天操妹子 | 亚洲国产婷婷香蕉久久久久久 | 中国一级毛片视频 | 日本免费一区二区三区毛片 | 久久一区精品 | 免费国产一级特黄aa大片在线 | 手机看黄av免费网址 | 日日操美女 | 亚洲天天网综合自拍图片专区 | 四虎永久网址在线观看 | 高清国产一级毛片国语 | 亚洲精品免费观看 | 日本免费的一级v一片 | 97影院97伦里片 | 在线日产一区二区 | 九九精品视频在线播放8 | 99热精品6| 男女午夜激情 | 欧美伊人久久大香线蕉综合69 | 久久99热精品免费观看k影院 | 中文字幕亚洲一区婷婷 | 日日爽视频 | 97xxxx| 久久婷婷五综合一区二区 | 爱操在线 | 精品国产一区二区三区久久影院 | 成人在激情在线视频 | 久久久久草 | 国产一级毛片视频在线! | 日韩欧美国产一区二区三区 | 国产精品视频国产永久视频 | 特级aaa毛片 | 一级毛片视频免费 | 人成午夜免费大片在线观看 |