欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Restful接口调用方法总结(超详细)

程序员文章站 2023-12-22 20:04:58
由于在实际项目中碰到的restful服务,参数都以json为准。这里我获取的接口和传入的参数都是json字符串类型。发布restful服务可参照文章 jersey实现res...

由于在实际项目中碰到的restful服务,参数都以json为准。这里我获取的接口和传入的参数都是json字符串类型。发布restful服务可参照文章 jersey实现restful服务(实例讲解),以下接口调用基于此服务。

基于发布的restful服务,下面总结几种常用的调用方法。

(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");
 }
}

结果:

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");
 }

}

结果:

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");
 }
}

结果:

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");

 }
}

结果:

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");
 }
}

结果:

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包引入有冲突,不能在一个工程中同时引用。

以上这篇基于restful接口调用方法总结(超详细)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: