日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/3 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在基于简单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>{@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的应用程序上为REST资源设置基本响应HTTP标头的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。