Vertx和Jersey集成使用

157 篇文章 5 订阅
29 篇文章 1 订阅

为了更好地解耦和提高性能,一般将工程的接口部分剥离出来形成一个单独的工程,这样不仅能提高性能,增强可维护性,并且在后台工程宕掉的话对客户端接口的影响较小。

公司使用了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 "上传成功";
    }

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值