2013-08-01 28 views
1

當我運行這段代碼時,我總是收到500錯誤,在我的生活中,我無法弄清楚爲什麼。如果任何人也可以幫助我弄清楚如何得到一個堆棧跟蹤打印出來,那也會很棒。下面是我的代碼(這是一個Maven項目):嵌入式碼頭應用程序中的500錯誤

編輯:不打印到服務器日誌,但我相信這是因爲碼頭嵌入

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 /方我得到的如下:

HTTP ERROR: 500 

Problem accessing /parties. Reason: 

    Request failed. 
Powered by Jetty:// 
+0

服務器日誌有說什麼嗎?這應該。將此添加到您的問題。 – Magnilex

+0

不,Jetty不會在服務器視圖下顯示,也不會顯示到服務器日誌,我相信這是因爲它是嵌入式的,我也運行了DropWizard API(嵌入式碼頭,但框架處理它,所以我沒有明確地寫它),我有它的作品,它不顯示在服務器之下或打印任何東西到服務器日誌 – sreya

回答

2

也找出了這個錯誤。基本上在我的setInitParam()方法中,我試圖調用一個不存在的包。 com.sun.jersey ...包在我的pom中不存在。添加後(並刪除玻璃魚)它完美的工作。

相關問題