在基于简单Vertx Rest的应用程序上为REST资源设置基本响应HTTP标头

我是Vert.x的新手,但是作为Java开发人员(非常努力),与NodeJS或其他任何基于Reactor的框架/库相比,我觉得它更加有趣并且很有前途。 因此,我正在使用Vert.x实现一个非常简单的Restful API。

今天的问题是我想在大多数(所有)响应中包含某些HttpHeaders。 例如,将Content-type设置为“ application / json”。 将来可能还会添加其他一些。

我有点想知道自己是Vert.x的新手,然后我才意识到, 本博客文章 (请参见BodyHandler的使用)最终提出的建议实际上对我有用

所以我有我的主要VertxMain java应用程序,在其中注册了MyWebVerticleApp

package com.javapapo.vertxweb;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;

/**
 * Created by <a href="mailto:javapapo@mac.com">javapapo</a> on 15/11/15.
 */
public class VertxEngineMain {
    public static void main(String[] args) {
        VertxOptions opts = new VertxOptions();
        Vertx vertx = Vertx.vertx(opts);
        vertx.deployVerticle(new MyWebVerticleApp());
    }



}

然后,我创建了一个小的处理程序,称为BaseResponseHandler ,该处理程序最终在响应中添加了HttpHeader

package com.javapapo.vertxweb.handlers;

import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;

/**
 * Created by <a href="mailto:javapapo@mac.com">javapapo</a> on 27/11/15.
 */
public class BaseResponseHandler implements Handler<RoutingContext>{

    @Override
    public void handle(RoutingContext context) {
        HttpServerResponse response = context.response();
        response.putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json");
        //other stuff!
        response.setChunked(true);
        context.next();
    }

}

然后,在MyWebVerticle我只是在路由器链接中注册要一直调用的处理程序。

package com.javapapo.vertxweb;

import com.javapapo.vertxweb.handlers.BaseResponseHandler;
import com.javapapo.vertxweb.handlers.StatusHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

/**
 * Created by <a href="mailto:javapapo@mac.com">javapapo</a> on 16/11/15.
 */
public class MyWebVerticleApp extends AbstractVerticle {
    @Override
    public void start(Future<Void> fut) {
        HttpServer server = vertx.createHttpServer();
        Router router = Router.router(vertx);
        //enable the base response handler overall!
        router.route().handler(new BaseResponseHandler());
        router.route("/status/").handler(new StatusHandler());
        server.requestHandler(router::accept).listen(8080);
    }
}

翻译自: https://www.javacodegeeks.com/2015/11/setting-basic-response-http-headers-rest-resources-simple-vertx-rest-based-app.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值