gpt4 book ai didi

java - 如何将 Jetty 和 Jersey 嵌入到我的 Java 应用程序中

转载 作者:太空狗 更新时间:2023-10-29 22:51:10 24 4
gpt4 key购买 nike

所以我尝试将 Jetty 嵌入到我的 Web 应用程序中,这样如果我将它打包为 jar,那么其他人就可以运行 jar 文件而不必担心配置服务器。但是,我在设置主类时遇到了一些问题,以便 jetty 可以访问我的资源类。我看过教程,但他们没有给我确切的信息。这是我到目前为止所拥有的。

package pojo;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class Main {

public static void main(String[] args) throws Exception {
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
ServletHolder h = new ServletHolder(new DefaultServlet());
h.setInitParameter("javax.ws.rs.Application","resources.DBCollection");
context.addServlet(h, "/*");
server.setHandler(context);
server.start();
server.join();
}
}

我正在尝试将其映射到此类:

package resources;


import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo;

import pojo.Party;

@Path("/parties")
public class DBCollection {

@Context
UriInfo url;

@Context
Request request;

String name;

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public List<Party> getAllParties() throws Exception
{
List<Party> list = new ArrayList<Party>();
list.addAll(DBConnection.getPartyCollection().values());
return list;
}

@GET
@Path("count")
@Produces(MediaType.TEXT_PLAIN)
public String getPartyCount() throws Exception
{
return String.valueOf(DBConnection.getPartyCollection().size());
}

@Path("{party}")
public DBResource getParty(@PathParam("party")String party)
{
return new DBResource(url,request,party);
}
}

这是我的 POM 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>PartyAPI</groupId>
<artifactId>PartyAPIMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.17.1</version>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0-SNAPSHOT</version>
</dependency>

</dependencies>
<repositories>
<repository>
<id>oss.sonatype.org</id>
<name>OSS Sonatype Staging</name>
<url>https://oss.sonatype.org/content/groups/staging</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.resteasy.Star.Main</Main-Class>

</manifestEntries>

</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

最佳答案

一些事情。

  1. Jetty 9.0.0.RC0 是一个旧的、尚未稳定的候选版本,请考虑升级到稳定的最终版本,例如 9.0.4.v20130625
  2. 您需要将 Jax RS 类连接到 servlet api 的东西。通常通过 Servlet 或使用您选择的库进行某种初始化来完成。 (在你的情况下 Jersey )

在您的示例中,您只设置了一个 DefaultServlet 来提供静态文件,没有任何配置来使用您的 DBCollection 对象。

对于 Jersey,您需要配置 org.glassfish.jersey.servlet.ServletContainer 并在您选择的上下文中设置其 servlet 映射。

示例:

package com.example;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class Main
{
public static void main(String[] args)
{
Server server = new Server(8080);

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
server.setHandler(context);

ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
jerseyServlet.setInitOrder(1);
jerseyServlet.setInitParameter("jersey.config.server.provider.packages","com.example");

ServletHolder staticServlet = context.addServlet(DefaultServlet.class,"/*");
staticServlet.setInitParameter("resourceBase","src/main/webapp");
staticServlet.setInitParameter("pathInfoOnly","true");

try
{
server.start();
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}
}
}

此示例将 jersey 提供的 ServletContainer 添加到 Jetty 用于根据传入请求查找要执行的操作的 ServletContextHandler。然后它添加 DefaultServlet 来处理 Jersey 不处理的任何内容请求(例如静态内容)

关于java - 如何将 Jetty 和 Jersey 嵌入到我的 Java 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17953886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com