REST 快速開發(fā)

2018-08-08 12:00 更新

然后能夠?qū)崿F(xiàn)快速開發(fā)呢?正如士兵需要的是一把好兵器,所以在我們開始開發(fā)之前就要選擇一款適用的 IDE,不管是哪款,只要你自己熟悉使用就好。本書中的實例使用的IDE是 Eclipse 。

安裝 M2Eclipse 插件

一般 Eclipse 都集成 Maven 的插件,如果沒有,要想在 Eclipse 中使用 Maven,需要 安裝 M2Eclipse 插件。有關(guān) M2Eclipse 的安裝可以參考http://www.eclipse.org/m2e/。安裝完后就能導(dǎo)入 Maven 項目,使用 Maven 的命令行。

rapid-dev-01

內(nèi)嵌 Servlet 容器

放了方便運行項目,可以將 Servlet 容器嵌入進項目中。下面談下幾種 常見的 Servlet 容器的嵌入。

嵌入 Tomcat

設(shè)置插件

<plugin>
    <groupId>org.apache.tomcat.maven</groupId>
    <artifactId>tomcat7-maven-plugin</artifactId>
    <version>${tomcat7.version}</version>
</plugin>

執(zhí)行

mvn tomcat7:run

rapid-dev-03

項目啟動成功,可以看到輸出:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building servlet-container 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> tomcat7-maven-plugin:2.2:run (default-cli) @ servlet-container >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ servlet-container ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspaceGithub\rest-in-action\samples\servlet-container\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ servlet-container ---
[INFO] Compiling 1 source file to D:\workspaceGithub\rest-in-action\samples\servlet-container\target\classes
[INFO] 
[INFO] <<< tomcat7-maven-plugin:2.2:run (default-cli) @ servlet-container <<<
[INFO] 
[INFO] --- tomcat7-maven-plugin:2.2:run (default-cli) @ servlet-container ---
[INFO] Running war on http://localhost:8080/servlet-container
[INFO] Creating Tomcat server configuration at D:\workspaceGithub\rest-in-action\samples\servlet-container\target\tomcat
[INFO] create webapp with contextPath: /servlet-container
三月 02, 2015 2:25:06 下午 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
三月 02, 2015 2:25:07 下午 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
三月 02, 2015 2:25:07 下午 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
三月 02, 2015 2:25:11 下午 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]

在瀏覽器里訪問 http://localhost:8080/servlet-container/ 就能看到主頁面了。

rapid-dev-04

嵌入 Jetty

設(shè)置插件

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
</plugin>

注意:使用該插件需要 Maven 3 和 Java 1.7 及以上版本

執(zhí)行

mvn jetty:run

rapid-dev-05

項目啟動成功,可以看到輸出:

[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building servlet-container 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:9.2.9.v20150224:run (default-cli) @ servlet-container >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ servlet-container ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspaceGithub\rest-in-action\samples\servlet-container\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ servlet-container ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ servlet-container ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workspaceGithub\rest-in-action\samples\servlet-container\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ servlet-container ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< jetty-maven-plugin:9.2.9.v20150224:run (default-cli) @ servlet-container <<<
[INFO] 
[INFO] --- jetty-maven-plugin:9.2.9.v20150224:run (default-cli) @ servlet-container ---
2015-03-02 15:06:54.654:INFO::main: Logging initialized @1764ms
[INFO] Configuring Jetty for project: servlet-container
[INFO] webAppSourceDirectory not set. Trying src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = D:\workspaceGithub\rest-in-action\samples\servlet-container\target\classes
[INFO] Context path = /
[INFO] Tmp directory = D:\workspaceGithub\rest-in-action\samples\servlet-container\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:/D:/workspaceGithub/rest-in-action/samples/servlet-container/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = D:\workspaceGithub\rest-in-action\samples\servlet-container\src\main\webapp
2015-03-02 15:06:54.713:INFO:oejs.Server:main: jetty-9.2.9.v20150224
2015-03-02 15:06:55.885:INFO:oejsh.ContextHandler:main: Started o.e.j.m.p.JettyWebAppContext@2863c{/,file:/D:/workspaceGithub/rest-in-action/samples/servlet-container/src/main/webapp/,AVAILABLE}{file:/D:/workspaceGithub/rest-in-action/samples/servlet-container/src/main/webapp/}
2015-03-02 15:06:55.886:WARN:oejsh.RequestLogHandler:main: !RequestLog
[INFO] Started Jetty Server
2015-03-02 15:06:55.911:INFO:oejs.ServerConnector:main: Started ServerConnector@1dde93{HTTP/1.1}{0.0.0.0:8080}
2015-03-02 15:06:55.912:INFO:oejs.Server:main: Started @3022ms

在瀏覽器里訪問 http://localhost:8080就能看到主頁面了。這里 Jetty 啟動項目是默認是不顯示項目名稱的。

rapid-dev-06

Source code 源碼

見 servlet-container。

參考:


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號