java - 嵌入式 Jetty 应用程序中出现 500 错误

标签 java maven jetty embedded-jetty

当我运行此代码时,我不断收到 500 错误,但我一生都无法找出原因。如果有人也可以帮助我弄清楚如何打印堆栈跟踪,那也很棒。这是我的代码(它是一个 Maven 项目):

编辑:服务器日志中没有打印任何内容,但我相信这是因为 Jetty 已嵌入

    package pojo;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.servlet.ServletContainer;

public class Main {

    public static void main(String[] args) throws Exception {
        Server server = new Server(8112);
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");
        server.setHandler(context);
        ServletHolder h = new ServletHolder(new ServletContainer());
        h.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
        h.setInitParameter("com.sun.jersey.config.property.packages", "resources");
        h.setInitOrder(1);
        context.addServlet(h, "/*");
        try
        {
            server.start();
            server.join();
        }
        catch (Throwable t)
        {
            t.printStackTrace(System.err);
        }
    }
    }

我正在尝试访问此资源:

package resources;


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

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;


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

import pojo.Party;

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

    @Context
    UriInfo url;

    @Context
    Request request;

    String name;

    public static final Timer allTime = DBConnection.registry.timer(MetricRegistry.name("Timer","all-parties"));

    @GET
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public List<Party> getAllParties() throws Exception
    {
        final Timer.Context context=allTime.time(); //start the timer 
        List<Party> list = new ArrayList<Party>();
        DBConnection.readAllData();
        list.addAll(DBConnection.getPartyCollection().values());
        context.stop(); //stops timer 
        return list;

//      ---> code for Jackson
//      String string; 
//      DBConnection.readAllData();
//      ObjectMapper jsonMapper = new ObjectMapper();
//      string=jsonMapper.writeValueAsString(DBConnection.getPartyCollection());
//      return string;
    }

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

    @Path("{party}") //points to OnePartyResource.class
    public OnePartyResource getParty(@PathParam("party")String party)
    {
        name = party;
        return new OnePartyResource(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.4.v20130625</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlet</artifactId>
            <version>9.0.4.v20130625</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-servlets</artifactId>
            <version>9.0.4.v20130625</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.6.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jax-rs-ri</artifactId>
            <version>2.0-m13</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.simple-spring-memcached</groupId>
            <artifactId>spymemcached</artifactId>
            <version>2.8.1</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</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>
</project>

当我输入 localhost:8112/partys 时,我得到以下信息:

   HTTP ERROR: 500

Problem accessing /parties. Reason:

    Request failed.
Powered by Jetty://

最佳答案

也解决了这个错误。本质上,在我的 setInitParam() 方法中,我试图调用一个不存在的包。我的 pom 中不存在 com.sun.jersey... 包。添加它(并删除 glassfish)后,它工作得很好。

关于java - 嵌入式 Jetty 应用程序中出现 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997144/

相关文章:

java - 如何让泛型函数根据Java中的输入获取对象属性

java - Maven 构建失败,无法在 org.springframework :spring-core:jar:4. 0.2.RELEASE 收集依赖项

java - 谷歌应用引擎 : couldn't start server - jetty

java - 针对特定目录的 Jetty9 基本身份验证

tomcat - 为最大并发请求提供服务的 Web 服务器

java - 静态初始化 block 和常规静态初始化之间的区别

双向 SSL 下的 Java 小程序

java - 在 build.gradle 中使用 java 变量

java - 下载 Maven 依赖项时校验和验证失败

java - 如何调试在 spring mvc rest 中找不到的 404 资源?