Vertx和Jersey集成使用
为了更好地解耦和提高性能,一般将工程的接口部分剥离出来形成一个单独的工程,这样不仅能提高性能,增强可维护性,并且在后台工程宕掉的话对客户端接口的影响较小。
公司使用了Vertx和Jersey,Vert.x是一个基于JVM、轻量级、高性能的应用平台,非常适用于最新的移动端后台、互联网、企业应用架构。Vert.x基于全异步Java服务器Netty,并扩展出了很多有用的特性;Jersey RESTful 框架是开源的RESTful框架, 实现了JAX-RS (JSR 311 & JSR 339) 规范。它扩展了JAX-RS 参考实现, 提供了更多的特性和工具, 可以进一步地简化 RESTful service 和 client 开发。尽管相对年轻,它已经是一个产品级的 RESTful service 和 client 框架。与Struts类似,它同样可以和hibernate,spring框架整合。程序中主要与jersey打交道。使用binder绑定filter和processor到内核中,做一些前置处理。基本的工程样子如下:
启动一个MainVerticle,用于整个程序的初始化和启动jersey服务器。
/**
* 运行方式:Windows:dy-server-api.bat run java-hk2:cn.esstx.dzg.api.tinyhttp.MainVerticle
* Linux: nohup ./bin/dy-server-api run java-hk2:cn.esstx.dzg.api.tinyhttp.MainVerticle &
* nohup ./bin/dy-server-api run java-hk2:cn.esstx.dzg.api.tinyhttp.MainVerticle -Dlogback.configurationFile=config/logback.xml &
*/
public class MainVerticle extends AbstractVerticle {
//日志框架最好使用slf4j,这样很好滴更换底层实现,例如使用log4j,logback,jdklog
public static void main(String[] args) {
// Create an HTTP server which simply returns "Hello World!" to each request.
Runner.runServer(MainVerticle.class);
}
@Override
public void start() throws Exception {
/*程序的其他一些初始化,我们工程就需要初始化JFinal和系统的配置文件*/
DeploymentOptions options = new DeploymentOptions();
String path = new PropertiesConfiguration("config/config.json").getPath();
//System.out.println(path);
JsonObject config = JsonConfigUtil.loadConfig(path);
options.setConfig(config);
vertx.deployVerticle("java-hk2:com.englishtown.vertx.jersey.JerseyVerticle", options);
}
}
public class Runner{
private static final String WEB_JAVA_DIR = "/src/main/java/";
public static void runClusteredServer(Class<?> clazz){
runServer(WEB_JAVA_DIR, clazz, new VertxOptions().setClustered(true), null);
}
public static void runServer(Class<?> clazz){
runServer(WEB_JAVA_DIR, clazz, new VertxOptions().setClustered(false), null);
}
public static void runServer(Class<?> clazz, DeploymentOptions options){
runServer(WEB_JAVA_DIR, clazz, new VertxOptions().setClustered(false), options);
}
public static void runServer(String dir, Class<?> clazz, VertxOptions options,
DeploymentOptions deploymentOptions){
runServer(dir + clazz.getPackage().getName().replace(".", "/"), clazz.getName(), options,
deploymentOptions);
}
public static void runScriptServer(String prefix, String scriptName, VertxOptions options){
File file = new File(scriptName);
String dirPart = file.getParent();
String scriptDir = prefix + dirPart;
runServer(scriptDir, scriptDir + "/" + file.getName(), options, null);
}
public static void runServer(String dir, String verticleID, VertxOptions options,
DeploymentOptions deploymentOptions){
if(options == null){
// Default parameter
options = new VertxOptions();
}
// Smart cwd detection
// Based on the current directory (.) and the desired directory
// (exampleDir), we try to compute the vertx.cwd
// directory:
try{
// We need to use the canonical file. Without the file name is .
File current = new File(".").getCanonicalFile();
if(dir.startsWith(current.getName()) && !dir.equals(current.getName())){
dir = dir.substring(current.getName().length() + 1);
}
}
catch(IOException e){
// Ignore it.
}
System.out.println(dir);
System.out.println(verticleID);
System.setProperty("vertx.cwd", dir);
Consumer<Vertx> runner = vertx -> {
try{
if(deploymentOptions != null){
vertx.deployVerticle(verticleID, deploymentOptions);
} else{
vertx.deployVerticle(verticleID);
}
}
catch(Throwable t){
t.printStackTrace();
}
};
if(options.isClustered()){
Vertx.clusteredVertx(options, res -> {
if(res.succeeded()){
Vertx vertx = res.result();
runner.accept(vertx);
} else{
res.cause().printStackTrace();
}
});
} else{
Vertx vertx = Vertx.vertx(options);
runner.accept(vertx);
}
}
}
{
"host": "localhost",
"port": 9090,
"base_path": "/rest/dy",
"resources": ["cn.esstx.dzg.api.tinyhttp.resources"],
"features": [],
//"binders": ["cn.esstx.dzg.api.tinyhttp.binder.RequestBinder"],//仅对filter有用
"hk2_binder": [
"com.englishtown.vertx.hk2.BootstrapBinder",
"cn.esstx.dzg.api.tinyhttp.binder.RequestBinder"
]
}
具体的使用方式:
@Path("/test")
public class TestResouce {
@GET
public String doGet() {
return "Hello World!";
}
@POST
@Produces(MediaType.APPLICATION_JSON)
public MyObject getJson() {
MyObject o = new MyObject();
o.setName("Andy");
return o;
}
@GET
@Path("jsonp")
@JSONP(queryParam = "cb")
@Produces("application/javascript")
public MyObject getJsonPadding() {
MyObject o = new MyObject();
o.setName("Andy");
return o;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public MyObject postJson(MyObject in) {
return in;
}
@POST
@Path("json")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public JsonObject postJson2(JsonObject in) {
return in;
}
@GET
@Path("async")
@Produces(MediaType.APPLICATION_JSON)
public void getJsonAsync(@Suspended final AsyncResponse asyncResponse, @Context Vertx vertx) {
vertx.runOnContext(aVoid -> {
MyObject o = new MyObject();
o.setName("Andy");
asyncResponse.resume(o);
});
}
public static class MyObject {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
////////////////测试Post和Get获取参数/////////////////
//get可以使用@Context、@QueryParam/////
//post可以使用@FormParam、MultivaluedMap<String, String> formParams
//都可以使用@DefaultValue/////////////
//QueryParam--url ? 后面表示的参数 . get post 通用.
//PathParam---url中的一部分,例如用{}表示的url中的一部分。get post 通用。
//FormParam---post提交的form表单参数。 用于 post
@GET
@Path("/dd")
public String getRequest(@Context HttpServerRequest request) {
String dd = request.params().get("dd");
System.out.println(dd);
return dd;
}
@GET
@Path("/ds")
public String getQuery(@QueryParam("dd") String dd) {
return dd;
}
@POST
@Path("/dd")
@Produces(MediaType.APPLICATION_JSON)
public String postForm(
@FormParam(value = "dd") String dd,
@FormParam(value = "ds") String ds) {
System.out.println(dd+ds);
return dd+ds;
}
@POST
@Path("/ds")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public String postRequest(
@DefaultValue("dd")
@FormParam("dd")
String dd) {
return dd;
}
@POST
@Path("/ss")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public String postJson(@DefaultValue("dd") @FormParam("dd") String dd) {
JsonObject object = new JsonObject();
object.put("dd", dd);
return object.toString();
}
@POST
@Path("/sd")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public String postContext(MultivaluedMap<String, String> formParams) {
String dd = formParams.getFirst("dd");
List<String> ds = formParams.get("ds");
return dd+ds.toString();
}
@POST
@Path("/sssss")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public String postQuery(@QueryParam("ss") String ss,@FormParam("dd") String dd) {
return dd+ss;//?ss=ss dd=dd
}
@POST
@Path("/sssss2")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
//http://localhost:9090/rest/dy/test/sssss2?ss=ss&sd=sd
public String postQuery2(MultivaluedMap<String, String> params) {
for(String ss:params.keySet()){
System.out.println(params.getFirst(ss));//只有form里面的
}
return ""+params.size();
}
@POST
@Path("/redirect")
@Produces(MediaType.TEXT_HTML)
public String redirect(){
return Utils.redirectTo("http://www.hao123.com");
}
}
/**
*
* @param location 需要重定向的位置
* @des 利用meta标签实现重定向
*/
public static String redirectTo(String location){
return "<!DOCTYPE><HTML><HEAD><TITLE>跳转页面</TITLE><meta http-equiv=\"refresh\" content=\"0; URL="+location+"\"></HEAD><BODY></BODY></HTML>";
}
上传文件:
添加jar
// https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart
compile group: 'org.glassfish.jersey.media', name: 'jersey-media-multipart', version: '2.17'
配置文件中:"features": ["org.glassfish.jersey.media.multipart.MultiPartFeature"]
DefaultJerseyHandler中的shouldReadData中添加
if(MediaType.MULTIPART_FORM_DATA_TYPE.getType().equalsIgnoreCase(mediaType.getType())
&& MediaType.MULTIPART_FORM_DATA_TYPE.getSubtype().equalsIgnoreCase(mediaType.getSubtype())){
return true;
}
<form action="http://127.0.0.1:8090/api/yyh/test/postFile" method="post" enctype="multipart/form-data">
<input type="file" name="postFile"/>
<input type="text" name="username"/>
<input type="submit" name="上传"/>
</form>
<form action="http://127.0.0.1:8090/api/yyh/test/postFile2" method="post" enctype="multipart/form-data">
<input type="file" name="postFile"/>
<input type="text" name="username"/>
<input type="submit" name="上传"/>
</form>
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/postFile")
// @Produces(MediaType.TEXT_PLAIN)
// public String postFile(@FormDataParam("postFile") InputStream stream,
// @FormDataParam("postFile") FormDataContentDisposition fileDisposition){
public String postFile(FormDataMultiPart form){
// 获取表单的其他数据
FormDataBodyPart usernamePart = form.getField("username");
System.out.println("name = " + usernamePart.getName() + " ,value = " + usernamePart.getValue());
// ContentDisposition headerOfFilePart =
// filePart.getContentDisposition();
// 把表单内容转换成流
try{
// 获取文件流
FormDataBodyPart filePart = form.getField("postFile");
System.out.println("MediaType = " + filePart.getMediaType());
InputStream fileInputStream = filePart.getValueAs(InputStream.class);
int len = 1024;// fileInputStream.available();
System.out.println("len = " + len);
byte[] buff = new byte[len];
len = fileInputStream.read(buff, 0, len);
System.out.println("content = " + new String(buff, 0, len));
FormDataContentDisposition formDataContentDisposition = filePart.getFormDataContentDisposition();
String source = formDataContentDisposition.getFileName();
String result = new String(source.getBytes("ISO8859-1"), "UTF-8");
System.out.println("result = " + result);
}
catch(IOException e1){
e1.printStackTrace();
}
return "上传成功";
}
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("/postFile2")
@Produces(MediaType.TEXT_PLAIN)
public String postFile2(@FormDataParam("postFile") InputStream stream,
@FormDataParam("postFile") FormDataContentDisposition fileDisposition,
@FormDataParam("username") String username){
System.out.println(username);
try{
// 这里坑死人了,不能用stream.available(),为0,available不可靠
java.io.File file = new java.io.File(fileDisposition.getFileName());
FileOutputStream stream2 = new FileOutputStream(file);
int len = 1024;
System.out.println("len = " + len);
byte[] buff = new byte[len];
len = stream.read(buff, 0, len);
stream2.write(buff, 0, len);
System.out.println("content = " + new String(buff, 0, len));
stream2.close();
System.out.println("fileName = " + fileDisposition.getFileName());
}
catch(IOException e1){
e1.printStackTrace();
}
return "上传成功";
}
Vertx和Jersey集成使用的更多相关文章
- Spring集成Jersey开发(附demo)
下文将会初步介绍如何在Spring中集成Jersey,并附简单的demo 所依赖的技术版本: Jersey 1.8 Spring 3.0.5.RELEASE 1. 项目依赖 pom.xml定义(注意去 ...
- 使用 Jersey 和 Apache Tomcat 构建 RESTful Web 服务
作者: Yi Ming Huang, 软件工程师, IBM Dong Fei Wu, 软件工程师, IBM Qing Guo, 软件工程师, IBM 出处: http://www.ibm.com/de ...
- 容易被忽视的后端服务 chunked 性能问题
容易被忽视的后端服务 chunked 性能问题 标签(空格分隔): springboot springmvc chunked 背景 spring boot 创建的默认 spring mvc 项目 集成 ...
- springboot配置文件priperties大全
flyway.baseline-description 执行基线时标记已有Schema的描述. flyway.baseline-on-migrate 在没有元数据表的情况下,针对非空Schema执行迁 ...
- spring boot application.properties 属性详解
2019年3月21日17:09:59 英文原版: https://docs.spring.io/spring-boot/docs/current/reference/html/common-appli ...
- Spring Boot属性文件配置文档(全部)
This sample file is meant as a guide only. Do not copy/paste the entire content into your applicatio ...
- spring rest 容易被忽视的后端服务 chunked 性能问题
spring boot 容易被忽视的后端服务 chunked 性能问题 标签(空格分隔): springboot springmvc chunked 作者:王清培(Plen wang) 沪江Java资 ...
- springboot中配置文件application.properties的配置详情,数据源配置
pring Boot使用了一个全局的配置文件application.properties,放在src/main/resources目录下或者类路径的/config下.Sping Boot的全局配置文件 ...
- springboot 配置文件说明
你可以在自己创建的组件上使用@ConfigurationProperties注解,而Spring Boot自动配置的很多组件也添加了@ConfigurationProperties注解,可以通过Spr ...
随机推荐
- 漫谈golang设计模式 工厂模式
工厂模式 意义:创建过程交给专门的工厂子类去完成.定义一个抽象的工厂类,再定义具体的工厂类来生成子类等,它们实现在抽象按钮工厂类中定义的方法.这种抽象化的结果使这种结构可以在不修改具体工厂类的情况下引 ...
- redis 进程使用root用户启动 -- 整改方案
最近内部风险整改, 各种进程使用root身份进行启动不符合要求, 于是各路神仙各施其法,为的就是让 某进程不以root 启动: 先以 redis 为例: 原有进程如下: #超一流标准的执行文件位置及配 ...
- 【error】C++:fatal error LNK1169: 找到一个或多个多重定义的符号
编译时报错 : fatal error LNK1169: 找到一个或多个多重定义的符号 解答: 发生这种错误就是在一个项目定义了多个main函数的问题. *C++中一个项目即一个程序,多个文件只能有一 ...
- Jenkins+Jmeter配置(Linux环境)
1.安装jenkins. 1.1在Linux服务器上,必须先安装jdk与Tomcat, 在/opt/tools/tomcat 安装解压Tomcat 1.2.在Linux服务器上安装jmeter 在/o ...
- Redis学习笔记(八、缓存设计)
目录: 缓存更新策略 缓存粒度 缓存穿透 缓存雪崩 缓存击穿 缓存更新策略: 1.内存溢出淘汰策略 当redis的使用内存超过maxmemory时会触发相应的策略,具体策略由maxmemory-pol ...
- 7、zabbix自定义监控阈值-前端页面报警
找个值监控一下: #监控passwd #默认是间隔是1小时,我们改成10秒,下面我们要把报警打开 #我们在被监控上的主机上创建一个新用户,过10秒,界面上就会报警了 ----------------- ...
- 七,专著研读(Logistic回归)
七,专著研读(Logistic回归) 分类:k-近邻算法,决策树,朴素贝叶斯,Logistic回归,支持向量机,AdaBoost算法. 运用 k-近邻算法,使用距离计算来实现分类 决策树,构建直观的树 ...
- day74_10_21 三大认证
一.权限六表. 一般在django中,基于用户权限访问控制的认证是RBAC(Role-Based Access Control) 还有一些基于auth的认证规则. Django框架采用的是RBAC认证 ...
- Mybatis动态SQL(五)
if choose (when, otherwise) trim (where, set) foreach 一.if 动态SQL通常要做的事情是有条件地包含 where 子句的一部分.比如: < ...
- 剑指Offer-8.跳台阶(C++/Java)
题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 分析: 实际上就是斐波那契数列的一个应用,青蛙跳上n级台阶的跳法数等于跳 ...