2016-07-10 93 views
0

我想運行一個REST服務的例子,使用Ubuntu 16.04,Oracle Java 8,Intellij與Maven,Jersey和Tomcat 8。可以在這裏找到https://medium.com/@jamsesso/starting-out-with-jersey-apache-tomcat-using-intellij-6338d93ffd40#.heb2u4ros。這個例子是我在使用Tomcat 8時使用Tomcat 7.當我運行代碼時,發送給我​​(但我認爲它應該是http://localhost:8080/hello我試過了,它也給我一個404)我沒有得到來自Intellij的任何警告,但我在網絡瀏覽器中獲得 HTTP Status 404 -type Status report message description The requested resource is not available. Apache Tomcat/8.0.32 (Ubuntu)。從命令行啓動Tomcat服務器可以正常工作,並向我發送歡迎消息。我經歷了所有類似的問題,我可以在stackoverflow找到,但我一直無法解決問題。對於我的代碼有什麼問題,我會非常高興。運行來自Intellij與Maven,Jersey和Tomcat的REST服務8

我的代碼如下。

web.xml 
<!DOCTYPE web-app PUBLIC 
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd" > 
<web-app> 

<servlet> 
    <servlet-name>Example API</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>com.example.jersey</param-value> 
    </init-param> 

    <init-param> 
     <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name> 
     <param-value>true</param-value> 
    </init-param> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Example API</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 
</web-app> 

_

pom.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<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>com.example.jersey</groupId> 
<artifactId>RESTServer</artifactId> 
<version>1.0-SNAPSHOT</version> 

<dependencies> 
    <dependency> 
     <groupId>com.sun.jersey</groupId> 
     <artifactId>jersey-bundle</artifactId> 
     <version>1.18.3</version> 
    </dependency> 
</dependencies> 
</project> 

_來自的IntelliJ

/usr/share/tomcat8/bin/catalina.sh run 
[2016-07-10 08:17:15,670] Artifact RESTServer:war exploded: Server is not connected. Deploy is not available. 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server version:  Apache Tomcat/8.0.32 (Ubuntu) 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server built:   Jul 6 2016 11:49:29 UTC 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Server number:   8.0.32.0 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: OS Name:    Linux 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: OS Version:   4.4.0-28-generic 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Architecture:   amd64 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Java Home:    /usr/lib/jvm/java-8-oracle/jre 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: JVM Version:   1.8.0_91-b14 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: JVM Vendor:   Oracle Corporation 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: CATALINA_BASE:   /home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: CATALINA_HOME:   /usr/share/tomcat8 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.util.logging.config.file=/home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2/conf/logging.properties 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcom.sun.management.jmxremote= 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcom.sun.management.jmxremote.port=1099 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcom.sun.management.jmxremote.ssl=false 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcom.sun.management.jmxremote.authenticate=false 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.rmi.server.hostname=127.0.0.1 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.endorsed.dirs=/usr/share/tomcat8/endorsed 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcatalina.base=/home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Dcatalina.home=/usr/share/tomcat8 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.VersionLoggerListener log 
INFO: Command line argument: -Djava.io.tmpdir=/home/mikael/.IntelliJIdea2016.1/system/tomcat/Unnamed_RESTServer_2/temp 
Jul 10, 2016 8:17:17 AM org.apache.coyote.AbstractProtocol init 
INFO: Initializing ProtocolHandler ["http-nio-8080"] 
Jul 10, 2016 8:17:17 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 
INFO: Using a shared selector for servlet write/read 
Jul 10, 2016 8:17:17 AM org.apache.catalina.startup.Catalina load 
INFO: Initialization processed in 1586 ms 
Jul 10, 2016 8:17:18 AM org.apache.catalina.core.StandardService startInternal 
INFO: Starting service Catalina 
Jul 10, 2016 8:17:18 AM org.apache.catalina.core.StandardEngine startInternal 
INFO: Starting Servlet Engine: Apache Tomcat/8.0.32 (Ubuntu) 
Jul 10, 2016 8:17:18 AM org.apache.coyote.AbstractProtocol start 
INFO: Starting ProtocolHandler ["http-nio-8080"] 
Jul 10, 2016 8:17:18 AM org.apache.catalina.startup.Catalina start 
INFO: Server startup in 188 ms 
Connected to server 
[2016-07-10 08:17:18,307] Artifact RESTServer:war exploded: Artifact is being deployed, please wait... 
Jul 10, 2016 8:17:19 AM org.apache.jasper.servlet.TldScanner scanJars 
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. 
[2016-07-10 08:17:19,467] Artifact RESTServer:war exploded: Artifact is deployed successfully 
[2016-07-10 08:17:19,468] Artifact RESTServer:war exploded: Deploy took 1,161 milliseconds 
+0

您的應用程序應該是在/ RESTServer。而且由於資源在/你好,在這個應用程序中,URL應該是/ RESTServer/hello –

+0

@JBNizet我嘗試了'http:// localhost:8080/RESTServer/hello',但我仍然得到了'404'。 – g3blv

+0

希望你的類包com.example.jersey位於文件夾結構RESTServer/src/main/java? – Sampada

回答

2

package com.example.jersey; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

@Path("/hello") 
public class HelloWorld { 
    @GET 
    @Produces(MediaType.TEXT_PLAIN) 
    public String getMessage() { 
     return "Hello world!"; 
    } 
} 

輸出我設法得到這個工作。主要問題是該示例使用的Servlet版本和Jersey版本的差異以及我能夠使用的版本。這個例子是在我使用3.1時使用Servlet 3.0。我可能在這裏錯了,但我認爲Servlet 3.1迫使我使用Jersey 2.這兩個差異意味着web.xmlpom.xml文件需要反映這些更改。我已在下面發佈了我的更新web.xmlpom.xml

web.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 

    <display-name>Archetype Created Web Application</display-name> 

    <servlet> 
     <servlet-name>Example API</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.example.jersey</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Example API</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

-

pom.xml 
<?xml version="1.0" encoding="UTF-8"?> 
<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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.example.jersey</groupId> 
    <artifactId>stack</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>javax.servlet-api</artifactId> 
      <version>3.1.0</version> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-servlet-core</artifactId> 
      <version>2.13</version> 
     </dependency> 
     <dependency> 
      <groupId>org.glassfish.jersey.containers</groupId> 
      <artifactId>jersey-container-servlet</artifactId> 
      <version>2.13</version> 
     </dependency> 
    </dependencies> 
    <build> 
     <finalName>stack</finalName> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-war-plugin</artifactId> 
       <version>2.5</version> 
      </plugin> 

      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.2</version> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <name>Stack</name> 
</project> 
相關問題