2013-04-29 36 views
3

我想從命令行使用內置的HTTP服務器運行一個簡單的澤西島應用程序。內置HTTP服務器的運行澤西島

繼各種教程,我設置我的應用程序像這樣:

的src/main/JAVA/NET/wjlafrance/jerseyfun/App.java:

package net.wjlafrance.jerseyfun; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.PathParam; 
import javax.ws.rs.core.Response; 

import java.io.IOException; 

import com.sun.jersey.api.container.httpserver.HttpServerFactory; 

/** 
* Hello world! 
* 
*/ 
@Path("/hello") 
public class App { 

    public static void main(String[] args) { 
     System.out.println("Starting HTTP server.."); 
     try { 
      HttpServerFactory.create("http://localhost:9998/").start(); 
     } catch (IOException ex) { 
      System.err.println(ex); 
     } 
    } 

    @GET 
    public Response getMessage() { 
     String output = "It works!"; 
     return Response.status(200).entity(output).build(); 
    } 

} 

的src /主/ web應用/ WEB-INF/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>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>net.wjlafrance.jerseyfun</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> 

當我運行mvn clean package exec:java -Dexec.mainClass=net.wjlafrance.jerseyfun.App,我看到這樣的輸出:

Starting HTTP server.. 
Apr 29, 2013 9:12:11 AM com.sun.jersey.api.core.ClasspathResourceConfig init 
INFO: Scanning for root resource and provider classes in the paths: 
    C:\cygwin\home\wlafrance\bin\apache-maven-3.0.5/boot/plexus-classworlds-2.4.jar 
Apr 29, 2013 9:12:11 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFO: Initiating Jersey application, version 'Jersey: 1.17 01/17/2013 03:31 PM' 
Apr 29, 2013 9:12:11 AM com.sun.jersey.server.impl.application.RootResourceUriRules <init> 
SEVERE: The ResourceConfig instance does not contain any root resource classes. 
[WARNING] 
java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297) 
     at java.lang.Thread.run(Thread.java:662) 
Caused by: com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. 
     at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99) 
     at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1331) 
     at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:168) 
     at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:774) 
     at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:770) 
     at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) 
     at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:770) 
     at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:172) 
     at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:264) 
     at com.sun.jersey.api.container.ContainerFactory.createContainer(ContainerFactory.java:246) 
     at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:117) 
     at com.sun.jersey.api.container.httpserver.HttpServerFactory.create(HttpServerFactory.java:92) 
     at net.wjlafrance.jerseyfun.App.main(App.java:22) 
     ... 6 more 

不夠清楚,我的服務器配置錯誤。有人能指引我朝着正確的方向嗎?

+0

你可以評論你使用的'pom.xml'嗎? – koppor 2017-03-23 21:24:29

回答

0

請在錯誤消息中查看此行「ResourceConfig實例不包含任何根資源類」? 您沒有爲http服務器設置任何資源。 我會做的是使用這種方法:

GrizzlyHttpServerFactory.createHttpServer("http://localhost:9998/", new Application()); 

新的應用程序()將創建一個新ResourceConfig類HTTP服務器。你應該檢查運動衫的文件,它只是一個包含java包的簡單類。我的ResourceConfig如下所示: import org.glassfish.jersey.server.ResourceConfig;

public class Application extends ResourceConfig { 
    public Application() { 
    packages("ftp.recourse"); 
    } 
} 

雖然ftp.recourse包包含所有的路徑和操作,如GET,PUT,POST。 查看球衣的官方文件以瞭解更多詳情。希望這將有助於

0

你應該做到以下幾點:

final com.sun.jersey.api.core.ResourceConfig packagesResourceConfig = new com.sun.jersey.api.core.ResourceConfig("net.wjlafrance.jerseyfun") ; 

HttpServerFactory.create("http://localhost:9998/", packagesResourceConfig).start(); 
+0

com.sun.jersey.api.core.ResourceConfig是抽象的;不能實例化 – koppor 2017-03-23 21:27:20

0

隨着新澤西1.x中,@pakOverflow點的答案正確的方向。這裏是我成功的完整代碼。沒有任何依賴Grizlly1或灰熊2等

import java.io.IOException; 
import java.util.HashSet; 
import java.util.Set; 

import javax.ws.rs.core.Application; 

import com.sun.jersey.api.container.httpserver.HttpServerFactory; 
import com.sun.jersey.api.core.DefaultResourceConfig; 
import com.sun.jersey.api.core.ResourceConfig; 

public class WineryUsingHttpServer { 

    public static void main(String[] args) throws IOException { 
     ResourceConfig packagesResourceConfig = new DefaultResourceConfig(); 
     Application app = new Application() { 
      @Override 
      public Set<Class<?>> getClasses() { 
       Set<Class<?>> res = new HashSet<>(); 
       res.add(org.example.MainResource.class); 
       return res; 
      } 
     }; 
     packagesResourceConfig.add(app); 

     HttpServerFactory.create("http://localhost:8080/", packagesResourceConfig).start(); 
    } 

}