没有合适的资源?快使用搜索试试~ 我知道了~
首页基于Restful接口调用方法总结(超详细)
基于Restful接口调用方法总结(超详细)
59 下载量 87 浏览量
更新于2023-03-16
评论
收藏 48KB PDF 举报
下面小编就为大家带来一篇基于Restful接口调用方法总结(超详细)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
资源详情
资源评论
资源推荐
基于基于Restful接口调用方法总结接口调用方法总结(超详细超详细)
下面小编就为大家带来一篇基于Restful接口调用方法总结(超详细)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟
随小编过来看看吧
由于在实际项目中碰到的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 + "");
}
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) + "");
}
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 + "");
}
}
结果:
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");
weixin_38712908
- 粉丝: 6
- 资源: 932
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0