2015-06-27 38 views
1

我有麻烦为我的桌面应用程序部署一个示例web服务到tomcat。如何将jax-rs web服务部署到apache tomcat?

这里是我的班

AppConfig.java

@ApplicationPath("sample-ws") 
public class AppConfig extends Application { 
    @Override 
    public Set<Class<?>> getClasses() { 
     Set<Class<?>> resources = new HashSet<Class<?>>(); 
     resources.add(HelloWorld.class); 
     return resources; 
    } 
} 

HelloWorld.java

@Path("hello-world") 
public class HelloWorld { 

    @GET 
    public String hello(){ 
     return "hello world"; 
    } 

} 

的pom.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://maven.apache.org/POM/4.0.0" 
     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.sample</groupId> 
    <artifactId>sample-ws</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <packaging>war</packaging> 

    <build> 
     <finalName>sample-ws</finalName> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>javax.ws.rs</groupId> 
      <artifactId>jsr311-api</artifactId> 
      <version>1.1.1</version> 
     </dependency> 
    </dependencies> 

</project> 

我不知道写什么在我的web.xml中,我可以'不要让这件事情起作用。我错过了什么?

P.S.如果这很重要,我使用apache-tomcat-8.0.23

回答

1

在web.xml中,您应该添加一个侦听器来启动框架,并为您的webservices添加url映射。看看这篇文章:http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/

然而,你的web.xml应该类似于此:

<web-app> 
<listener> 
    <listener-class> 
      com.sun.xml.ws.transport.http.servlet.WSServletContextListener 
    </listener-class> 
</listener> 
<servlet> 
    <servlet-name>hello</servlet-name> 
    <servlet-class> 
     com.sun.xml.ws.transport.http.servlet.WSServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>hello</servlet-name> 
    <url-pattern>/hello</url-pattern> 
</servlet-mapping>