尽管很长一段时间以来我一直在使用Maven,但直到最近我才使用过Jetty插件。 为了能够测试REST客户端,我创建了一个Servlet,向我显示了所有传入的参数和带有传入请求的标头。 为了在容器中运行servlet,我决定尝试使用Maven Jetty插件 。 所以首先我使用特定的Maven原型创建一个Web应用程序:
mvn archetype:generate -DgroupId=net.pascalalma -DartifactId=rest-service -Dversion=1.0.0-SNAPSHOT -DarchetypeArtifactId=maven-archetype-webapp
这将导致完整的项目和以下日志记录:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.2:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
Downloading: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar
Downloaded: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.jar (4 KB at 5.2 KB/sec)
Downloading: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom
Downloaded: http://artifactory.redstream.nl/repo/org/apache/maven/archetypes/maven-archetype-webapp/1.0/maven-archetype-webapp-1.0.pom (533 B at 1.1 KB/sec)
[INFO] Using property: groupId = net.pascalalma
[INFO] Using property: artifactId = rest-service
[INFO] Using property: version = 1.0.0-SNAPSHOT
[INFO] Using property: package = net.pascalalma
Confirm properties configuration:
groupId: net.pascalalma
artifactId: rest-service
version: 1.0.0-SNAPSHOT
package: net.pascalalma
Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-webapp:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: net.pascalalma
[INFO] Parameter: packageName, Value: net.pascalalma
[INFO] Parameter: package, Value: net.pascalalma
[INFO] Parameter: artifactId, Value: rest-service
[INFO] Parameter: basedir, Value: /Users/pascal/projects
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /Users/pascal/projects/rest-service
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.057s
[INFO] Finished at: Sun Feb 03 17:13:33 CET 2013
[INFO] Final Memory: 7M/81M
[INFO] ------------------------------------------------------------------------
MacBook-Air-van-Pascal:projects pascal$
接下来,我将servlet代码添加到项目中:
package net.pascalalma.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author pascal
*/
public class TestRestServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println('GET method called');
out.println('parameters:\n ' + parameters(request));
out.println('headers:\n ' + headers(request));
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println('POST method called');
out.println('parameters: ' + parameters(request));
out.println('headers: ' + headers(request));
}
public void doDelete(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println('Delete method called');
}
private String parameters(HttpServletRequest request) {
StringBuilder builder = new StringBuilder();
for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
builder.append('|' + name + '->' + request.getParameter(name)+'\n');
}
return builder.toString();
}
private String headers(HttpServletRequest request) {
StringBuilder builder = new StringBuilder();
for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) {
String name = (String) e.nextElement();
builder.append('|' + name + '->' + request.getHeader(name)+'\n');
}
return builder.toString();
}
}
并在“ web.xml”中配置servlet。 顺便说一下,生成的“ web.xml”无法显示在我的Netbeans版本(v7.2.1)中。 我收到消息:
Web应用程序版本不受支持。 将web.xml升级到2.4或更高版本,或使用以前的NetBeans版本
为了解决这个问题,我修改了web.xml,使其从以下命名空间声明开始:
<web-app xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://java.sun.com/xml/ns/javaee'
xmlns:web='http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'
xsi:schemaLocation='http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd' id='WebApp_ID' version='2.5'>
接下来,将servlet添加到修改后的“ web.xml”中:
<?xml version='1.0' encoding='UTF-8'?>
...
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>TestRestServlet</servlet-name>
<servlet-class>net.pascalalma.servlets.TestRestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestRestServlet</servlet-name>
<url-pattern>/TestRestServlet</url-pattern>
</servlet-mapping>
...
现在一切准备就绪,可以测试servlet。 正如我之前所说的,我将为此使用Jetty插件。 要将插件添加到项目中,只需将以下内容放入“ pom.xml”中:
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<contextPath>/</contextPath>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>STOP</stopKey>
<stopPort>8005</stopPort>
<port>8080</port>
</configuration>
</plugin>
</plugins>
现在,我可以在终端中运行命令“ mvn jetty:run”,以使容器运行servlet。 日志应该以以下内容结尾:
....
2013-02-19 09:54:53.044:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 10 seconds.</code>
现在,如果您打开浏览器并转到该URL'http:// localhost:8080 / TestRestServlet?bla = true&#8217 ; 您将看到servlet的运行并输出到浏览器:
GET method called
parameters:
|bla->true
headers:
|DNT->1
|Host->localhost:8080
|Accept->text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
|Accept-Charset->ISO-8859-1,utf-8;q=0.7,*;q=0.3
|Accept-Language->en-US,en;q=0.8
|User-Agent->Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
|Connection->keep-alive
|Cache-Control->max-age=0
|Accept-Encoding->gzip,deflate,sdch
注意事项:如您所见,在插件配置中,为方便起见,我添加了一些额外的参数。 因此,容器将每隔10秒检查一次servlet中的更改,因此我不必在每次更改servlet之后重新启动Jetty容器。 要停止容器,您现在可以在另一个终端会话中输入命令'mvn jetty:stop -DstopPort = 8005 -DstopKey = STOP'。 顺便说一句,请确保将插件命名为“ jetty-maven-plugin”,而不是“ maven-jetty-plugin”,因为那样您将使用该插件的旧版本,而该插件不会使用配置参数(是的,非常令我感到困惑和沮丧)。
参考:在The Pragmatic Integrator博客上使用我们JCG合作伙伴 Pascal Alma的Maven Jetty插件 。
翻译自: https://www.javacodegeeks.com/2013/03/using-maven-jetty-plugin.html