RestEasy编写API
我是从这篇文章学习的RESTEASY
https://www.cnblogs.com/langtianya/p/7624647.html
1. 现实需求
任务:发送请求给服务器,解析请求,返回结果
请求内容:user_id, 图片
2. 分析
1)首先要指定url,参数列表,请求方式,提供一个对外开放接口;
2)业务逻辑在接口中,返回数据;
3) 调用方会根据提供的接口url,指定的参数列表,请求方式post/get调用接口。
3. 实现过程
1) pom.xml部署依赖
<dependency><groupId>org.jboss.resteasy</groupId><artifactId>jaxrs-api</artifactId><version>3.0.7.Final</version></dependency><dependency><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-jaxrs</artifactId><version>3.0.19.Final</version><!--<scope>provided</scope>--></dependency><dependency><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-client</artifactId><version>3.0.19.Final</version></dependency><dependency><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-multipart-provider</artifactId><version>3.0.19.Final</version><scope>provided</scope></dependency><dependency><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-jackson2-provider</artifactId><version>3.0.19.Final</version></dependency><dependency><groupId>org.jboss.resteasy</groupId><artifactId>resteasy-servlet-initializer</artifactId><version>3.0.19.Final</version></dependency>
2)实现API接口 TestRest
思路
图片url是:“http://pic38.nipic.com/” +id+"/"+pic_name
得到一张图片
将图片另存到本地,并得到以时间戳命名的名称
返回fin = pic + id
代码:
package Rest;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;/*** 用来处理请求的API* @author bobo**/
@Path("/path1")
public class TestRest {@GET@Produces("application/json")@Path("subpath/{id}/{pic_name}")public String get(@PathParam("id") String id , @PathParam("pic_name") String pic_name) throws IOException{//String result = "接收参数: " + id +" " + pic_name;URL url1 = new URL("http://pic38.nipic.com/" +id+"/"+pic_name);URLConnection uc = url1.openConnection();InputStream inputStream = uc.getInputStream();String path = "D:\\image\\"+System.currentTimeMillis()+".jpg";FileOutputStream out = new FileOutputStream(path);int j = 0;while ((j = inputStream.read()) != -1) {out.write(j);}inputStream.close();//得到picString pic = path.substring(9, path.length()-4);String fin = pic + " " + id;System.out.println(pic + " " + id);return fin;}
}
测试:
运行程序,tomcat启动
输入url http://localhost:8080/api_test_1/path1/subpath/20140225/2531170_214014788000_2.jpg
返回:
3) web.xml配置
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN""http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app><display-name>Archetype Created Web Application</display-name><context-param><param-name>resteasy.resources</param-name><param-value>Rest.TestRest</param-value></context-param><listener><listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class></listener><servlet><servlet-name>Resteasy</servlet-name><servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class></servlet><servlet-mapping><servlet-name>Resteasy</servlet-name><url-pattern>/*</url-pattern></servlet-mapping>
</web-app>
4)测试API接口 TestRestAPI
代码:
package Rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;public class TestRestAPI{ public static final String USER_API = "http://localhost:8080/api_2/path1/subpath/20140225/2531170_214014788000_2.jpg"; public static String getPic() throws IOException{//列出原始数据StringBuilder json = new StringBuilder(); URL url = new URL(USER_API); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setConnectTimeout(1000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String inputLine = null; while ( (inputLine = in.readLine()) != null){ json.append(inputLine); } String Strjson=json.toString();return Strjson.toString().split(" ")[0];}public static String getUid() throws IOException{//列出原始数据StringBuilder json = new StringBuilder(); URL url = new URL(USER_API); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setConnectTimeout(1000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String inputLine = null; while ( (inputLine = in.readLine()) != null){ json.append(inputLine); } String Strjson=json.toString();return Strjson.toString().split(" ")[1];}public static void main(String[] args) throws IOException {//列出原始数据StringBuilder json = new StringBuilder(); URL url = new URL(USER_API); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setConnectTimeout(1000); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8")); String inputLine = null; while ( (inputLine = in.readLine()) != null){ json.append(inputLine); } String Strjson=json.toString();String[] str = Strjson.toString().split(" ");String pic_name = str[0];String id = str[1];int user_id = Integer.parseInt(id);System.out.println("pic_name:");System.out.println(pic_name); System.out.println("id:"); System.out.println(user_id);}
}
测试:
运行程序,tomcat启动
给定url
http://localhost:8080/api_test_1/path1/subpath/20140225/2531170_214014788000_2.jpg
两个方法:
getPic() 得到:1571794041366
getUid() 得到:20140225
主函数得到:
pic_name:
1571794041366
id:
20140225
RestEasy编写API相关推荐
- php 动态彩码辨色 接口的调用_好用的云函数!后端低代码接口开发,零基础编写API接口...
前言 在开发项目过程中,经常需要用到API接口,实现对数据库的CURD等操作. 不管你是专业的PHP开发工程师,还是客户端开发工程师,或者是不懂编程但懂得数据库SQL查询,又或者是完全不太懂技术的人, ...
- Python学习笔记:Day 9 编写API
前言 最近在学习深度学习,已经跑出了几个模型,但Pyhton的基础不够扎实,因此,开始补习Python了,大家都推荐廖雪峰的课程,因此,开始了学习,但光学有没有用,还要和大家讨论一下,因此,写下这些帖 ...
- python Flask 编写 api 接口,CORS 解决 flask 跨域问题
为什么要编写 API 接口 flask 本身就是一个web框架,完全可以通过内嵌的方式使用python flask框架完成 web 页面的开发. 因为现在都讲究一个前后端分离,那为什么要前后端分离呢, ...
- (上)vuepress编写API文档verdaccio搭建并发布组件库
vuepress编写API文档 介绍 搭建 安装 初始化 配置 组件API文档 发布组件库 坑 没有高亮 介绍 想要搭建一个类似elementUI的组件库及相关的API文档,可以直接引用组件,还有代码 ...
- 好用的云函数!后端低代码接口开发,零基础编写API接口
前言 在开发项目过程中,经常需要用到API接口,实现对数据库的CURD等操作. 不管你是专业的PHP开发工程师,还是客户端开发工程师,或者是不懂编程但懂得数据库SQL查询,又或者是完全不太懂技术的人, ...
- 使用Pandaria编写API自动化测试进阶用法
简介 Pandaria是一款基于Cucumber JVM的API自动化测试工具,上一篇文章介绍了它的基本功能,包括基本的HTTP操作和数据库操作.本文将介绍使用Pandaria编写自动化测试的一些高级 ...
- python实现简单的api接口-使用Python编写API接口和使用API接口
本文中的代码部分有乱码情况,请点击此链接点击查看,或者关注微信公众号"微电脑'查看. Get 方法实现(代码中已经注释明白)# coding:utf-8 import json from u ...
- Node编写API接口,ajax实现增删改查
首先展示一下预览效果以及文件结构: 一.预览效果: 信息列表: 编辑: 新增: 删除: 二.代码结构: 文件说明: 1.css文件:存放项目页面样式 2.json:用于项目数据存储 3.index.h ...
- 后端如何编写API文档给到前端?
API文档如何写? learn [连载]the first time项目日志(一)--做项目查资料 =>转载自博客http://blog.itpub.net/31562041/viewspace ...
最新文章
- django中的缓存 单页面缓存,局部缓存,全站缓存 跨域问题的解决
- 月亮之上--数学分析版
- Spring框架—体系结构
- SAP Gateway OData服务的语言决定逻辑 - language determination
- Windows如何开启虚拟化,以安装虚拟机?
- linux aemv7,无法在我的Ubuntu machin中安装“xlwings”
- Serena Dimensions 介绍
- OpenShift 4 之 Hello-OpenShift
- Java Servlet request
- PHP泡泡龙源码,JS泡泡龙游戏网页版+完整代码
- 获取map中的一个value值以及遍历map获得map里所有key、value的值
- 交换机的VLAN与Trunk配置
- iphone电池怎么保养_手机电池不耐用怎么办啊?平时要怎么保养呢?我来告诉你...
- mysql8 bigint类型和datetime类型的转换
- 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址
- STM32单片机基础知识总结(二)
- JavaSE就业班四----数据库Oracle和JDBC
- 台式计算机怎么开声音,台式机如何使用耳机说话
- html5 文字滑动效果,jQuery滑动文字特效
- java计算机毕业设计口红专卖网站MyBatis+系统+LW文档+源码+调试部署