我已经下载了一个教程并对其进行了一些修改以满足我的需要(添加了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,这允许我与服务进行交互。
谢谢你的时间。
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();
}
}
范例来自:http : //www.nikgrozev.org/2014/10/16/rest-with-embedded-jetty-and-jersey-in-a-single-jar-step-by-step/
您还可以使用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();
另请参阅:使用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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句