vertx rest 跨域_在基于简单Vertx Rest的应用程序上为REST资源设置基本响应HTTP标头...
vertx rest 跨域
我是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>{@Overridepublic 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 {@Overridepublic 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
vertx rest 跨域
總結
以上是生活随笔為你收集整理的vertx rest 跨域_在基于简单Vertx Rest的应用程序上为REST资源设置基本响应HTTP标头...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黄油怎样用 黄油的用途十分广泛
- 下一篇: 如何防止手机电池爆炸