最近是真的忙,好久沒寫了,再來分享一點新東西?。?!
一、 新建gradle項目
①
②選擇gradle(如果沒有安裝gradle,自己下載一個)
③
④選擇gradle
下一步,然后輸入項目名稱和磁盤路徑,點擊finish。
二、配置vertx依賴
項目打開之后,在build.gradle文件中dependencies里面加入vertx的核心依賴
1
|
compile 'io.vertx:vertx-core:3.4.2' |
在build.gradle最下面加入任務
1
2
3
4
|
task copyjars(type: copy) { from configurations.runtime into 'lib' // 目標位置 } |
build.gradle內容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
group 'test' version '1.0-snapshot' apply plugin: 'java' sourcecompatibility = 1.5 repositories { mavencentral() } dependencies { compile 'io.vertx:vertx-core:3.4.2' testcompile group: 'junit' , name: 'junit' , version: '4.11' } task copyjars(type: copy) { from configurations.runtime into 'lib' // 目標位置 } |
執行這個任務(命令行 gradle copyjars或者在右側找copyjars雙擊),會將依賴jar下載到項目根目錄下的lib目錄
然后右擊lib –> add as library…
如果沒有依賴就會報錯
三、 創建java項目
①創建module
②創建class
創建web服務的方式
1、直接main方法啟動
1
2
3
4
5
6
7
8
|
import io.vertx.core.vertx; public class app1 { public static void main(string[] args) { vertx.vertx().createhttpserver().requesthandler(req -> req.response(). end( "hello vertx!" )).listen( 8989 ); } } |
在地址欄輸入 localhost:8989就可以看到hello vertx!
2、繼承application重寫start方法
1
2
3
4
5
6
7
8
9
10
11
|
import io.vertx.core.vertx; import javafx.application.application; import javafx.stage.stage; public class app2 extends application { @override public void start(stage primarystage) throws exception { vertx.vertx().createhttpserver().requesthandler(req -> req.response(). end( "hello my application!" )).listen( 8888 ); } } |
3、繼承abstractverticle重寫start方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import io.vertx.core.abstractverticle; import io.vertx.core.vertx; public class app3 extends abstractverticle { @override public void start() { vertx.vertx() .createhttpserver() .requesthandler(r -> { r.response().end( "hello verticle !!!" ); }) .listen( 8787 ); } public static void main(string[] args) { app3 app = new app3(); app.start(); } } |
通過main方法啟動
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/tao_ssh/article/details/78318858