vert.x 结合JAX-RS

Coding a simple REST Service with Vert-x

We will now run a bit more advanced example which will leverage a REST Service. This service, exposes a @GET Resource which will print out the parameter passed on the PATH URL of your Web application:

 

 

 

package org.demo.vertx;

import javax.ws.rs.GET;

import javax.ws.rs.Path;

import javax.ws.rs.PathParam;

import javax.ws.rs.core.Response;

@Path("/")

public class HelloWorldService {

  @GET

  @Path("/{name:.*}")

  public Response doGet(@PathParam("name") String name) {

    if (name == null || name.isEmpty()) {

      name = "World";

    }

    return Response.status(200).entity("Hello " + name).build();

  }

}

Some changes will be required in your Verticle as well to build the JAX-RS controller deployment, adding to the JAX-RS registry the HelloWorldService:

 

package org.demo.vertx;

import io.vertx.core.AbstractVerticle;

import org.jboss.resteasy.plugins.server.vertx.VertxRequestHandler;

import org.jboss.resteasy.plugins.server.vertx.VertxResteasyDeployment;

public class DemoV extends AbstractVerticle {

  @Override

  public void start() throws Exception {

    VertxResteasyDeployment deployment = new VertxResteasyDeployment();

    deployment.start();

    deployment.getRegistry().addPerInstanceResource(HelloWorldService.class);

    // Start the front end server using the Jax-RS controller

    vertx.createHttpServer()

        .requestHandler(new VertxRequestHandler(vertx, deployment))

        .listen(8080, ar -> {

          System.out.println("Server started on port "+ ar.result().actualPort());

        });

  }

}

In order to be able to build it, you need to include resteasy vert-x dependency:

1

2

3

4

5

<dependency>

   <groupId>org.jboss.resteasy</groupId>

   <artifactId>resteasy-vertx</artifactId>

   <version>3.1.0.Final</version>

</dependency>

Let's test it!

1

2

$ curl http://localhost:8080/Frank

Hello Frank

转载于:https://my.oschina.net/masterworker/blog/1098888

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值