我使用tomcat和jersey建立了休息服务。
使用过的web.xml仅包含:
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
pom.xml文件包含:
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
<layout>default</layout>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.17</version>
</dependency>
</dependencies>
</project>
这是我用作服务实现的java文件的一部分:
@Path("/light")
public class Hello {
// This method is called if TEXT_PLAIN is requested
@GET
@Path("/state/")
@Produces(MediaType.TEXT_PLAIN)
public String getStateOfLights() {
return "hi";//getLightsStateAsJSON();
}
}
直接位于WebContent目录下的index.html文件可通过localhost:8080 / ha / index.html访问。这是按预期工作的。
如果我想测试我的休息服务,请致电:http://localhost:8080/ha/rest/light/state/ 我得到了什么:HTTP状态404:未找到
我在这里缺少什么?
这是服务器日志:
Apr 25, 2015 7:07:32 PM org.apache.tomcat.util.digester.SetPropertiesRule begin WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:ha' did not find a matching property.
Apr 25, 2015 7:07:32 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/werner/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
Apr 25, 2015 7:07:32 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Apr 25, 2015 7:07:32 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Apr 25, 2015 7:07:32 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 620 ms
Apr 25, 2015 7:07:32 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 25, 2015 7:07:32 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.59
Apr 25, 2015 7:07:33 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [548] milliseconds.
Apr 25, 2015 7:07:34 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Apr 25, 2015 7:07:34 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Apr 25, 2015 7:07:34 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2641 ms
答案 0 :(得分:2)
您的web.xml中缺少<init-param>
。它应该是:
<servlet>
<servlet-name>Jersey REST Service</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</param-value> <!-- where com.example.Hello is your Hello class -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
将com.example
替换为Hello
类的包。