我已经下载了一个教程,并根据我的需要对其进行了一些修改(添加了maven)
我只是想知道是什么让服务从特定主页开始 - 当我运行我的服务时,它默认为以下
http://localhost:8080/RESTfulExample/WEB-INF/classes/com/ricki/test/JettyService.java
我的web.xml如下所示
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.ricki.test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
我的码头服务类看起来像这样
import com.google.common.util.concurrent.AbstractIdleService;
import java.lang.management.ManagementFactory;
import org.eclipse.jetty.jmx.MBeanContainer;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
public class JettyService extends AbstractIdleService
{
private Server server;
@Override
protected void startUp() throws Exception
{
server = new Server(8080);
MBeanContainer mbContainer = new MBeanContainer(ManagementFactory.getPlatformMBeanServer());
server.addBean(mbContainer);
Resource resource = Resource.newClassPathResource("/webapp");
WebAppContext context = new WebAppContext(resource.getURL().toExternalForm(), "/ricki-test/");
server.setHandler(context);
server.start();
}
@Override
protected void shutDown() throws Exception
{
try
{
server.stop();
server.join();
}
finally
{
server.destroy();
}
}
}
任何我的休息类看起来如下
@Path("/hello")
public class HelloWorldService
{
private final Logger logger = Logger.getLogger(HelloWorldService.class);
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg)
{
logger.info("Received message " + msg);
String output = "Hi : " + msg;
return Response.status(200).entity(output).build();
}
}
我希望我的主页设置为http://localhost:8080/RESTfulExample/,显示我的主页或实际上http://localhost:8080/RESTfulExample/rest/hello/ricki,这样我就可以与我的服务进行互动。
感谢您的时间。
答案 0 :(得分:7)
如果您不想,则无需使用web.xml
文件。如果您使用的是嵌入式Jetty服务器,则可以手动连接到Jersey:
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
Server jettyServer = new Server(8080);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(
org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter(
"jersey.config.server.provider.classnames",
EntryPoint.class.getCanonicalName());
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
}
您还可以使用jersey-container-jetty-http
依赖关系:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.23.1</version>
</dependency>
这允许你这样做:
URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
ResourceConfig config = new ResourceConfig(MyResource.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
如果你真的想使用web.xml
,你应该以不同的方式访问它:
Server server = new Server(8080);
String rootPath = SimplestServer.class.getClassLoader().getResource(".").toString();
WebAppContext webapp = new WebAppContext(rootPath + "../../src/main/webapp", "");
server.setHandler(webapp);
server.start();
server.join();
另请参阅:Configure embedded jetty with web.xml?
此时,使用Jetty maven插件更容易,该插件捆绑了war文件并将其部署到本地Jetty服务器:
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.6.v20151106</version>
<configuration>
<scanTargets>
<scanTarget>${project.basedir}/src/main</scanTarget>
<scanTarget>${project.basedir}/src/test</scanTarget>
</scanTargets>
<webAppConfig>
<contextPath>/${project.artifactId}-${project.version}</contextPath>
</webAppConfig>
<contextPath>${project.artifactId}</contextPath>
</configuration>
</plugin>