2016-10-10 280 views
0

我想要使用Eclipse和Maven在Tomcat上運行一個簡單的webservice。WebService - tomat/eclipse/maven - 請求的資源不可用

我一直在收到此消息:404請求的資源不可用。

服務器運行良好,沒有錯誤信息。在主機管理器中,該網站顯示爲正在運行。點擊它給...一個404

控制器是:

package nl.deholtmans.webservice; 
import javax.jws.WebMethod; 
import javax.jws.WebParam; 
import javax.jws.WebService; 
import javax.jws.soap.SOAPBinding; 

@WebService 
@SOAPBinding(style = SOAPBinding.Style.RPC) 
public class HelloWebService { 
    @WebMethod(operationName = "sayHello") 
    public String sayHello(@WebParam(name="guestname") String guestname){ 
     if(guestname==null){ 
      return "Hello"; 
     } 
     return "Hello "+ guestname; 
    } 
} 

WEB-INF /太陽jaxws.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> 
<endpoint name="HelloWebService" implementation="nl.deholtmans.webservice.HelloWebService" url-pattern="/helloWebService" ></endpoint> 
</endpoints> 

WEB-INF/web.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

<display-name>jaxwsExample</display-name> 
<listener> 
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>helloWebService</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>helloWebService</servlet-name> 
    <url-pattern>/helloWebService</url-pattern> 
</servlet-mapping> 
<session-config> 
<session-timeout>120</session-timeout> 
</session-config> 
</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>HelloWebService</groupId> 
<artifactId>HelloWebService</artifactId> 
<packaging>war</packaging> 
<version>1.0-SNAPSHOT</version> 
<name>JAX-WS webservice with maven</name> 
<dependencies> 
    <dependency> 
     <groupId>com.sun.xml.ws</groupId> 
     <artifactId>jaxws-rt</artifactId> 
     <version>2.1.3</version> 
    </dependency> 
</dependencies> 
<build> 
    <finalName>HelloService</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>1.6</source> 
       <target>1.6</target> 
       <encoding>UTF-8</encoding> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 
</project> 

在Eclipse中我通常會做(例如,春MVC):

[1]項目>清潔 [2]運行方式> Maven的清潔 [3]運行爲> Maven的安裝 [4]運行方式>在服務器上運行

我應該測試它與以下URL: http://localhost:8080/HelloWebService/helloWebService

沒有錯誤。沒有堆棧跟蹤。只有404,沒有資源可用。通過Tomcat管理器,我發現服務正在運行。

+1

您可以提供堆棧跟蹤和代碼的目錄結構b ASE? –

+0

沒有堆棧跟蹤。一切都成功部署。另外,localhost:8080顯示主頁面。 「網站」HelloWebService有指示運行。 – tjm1706

回答

0

通過另一個網站,我看到了正確的實施。 @RITZ XAVI - 感謝您將我指向文件夾結構。我將web.xml放在WebContents下(而不是webapp)。

最終溶液是正確的是:

[1] web服務接口:

@WebService 
@SOAPBinding(style = Style.RPC) 
public interface HelloWorld{ 
    @WebMethod String getHelloWorldAsString(); 
} 

[2] web服務實現:

@WebService(endpointInterface = "nl.deholtmans.webservice.HelloWorld") 
public class HelloWorldImpl implements HelloWorld{ 
    @Override 
    public String getHelloWorldAsString() { 
     return "Hello World JAX-WS"; 
    } 
} 

[3]太陽jaxws.xml (在webcontent/WEB-INF下):

<?xml version="1.0" encoding="UTF-8"?> 
<endpoints 
    xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" 
    version="2.0"> 
<endpoint 
    name="HelloWorld" 
    implementation="nl.deholtmans.webservice.HelloWorldImpl" 
    url-pattern="/hello"/> 
</endpoints> 

[4]的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
    Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"> 
<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> 
<session-config> 
    <session-timeout>120</session-timeout> 
</session-config> 
</web-app> 

[5]的pom.xml(Maven的)...只改變:

<build> 
    <finalName>HelloService</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.2</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.4</version> 
      <configuration> 
       <webXml>WebContent\WEB-INF\web.xml</webXml> 
       <warSourceDirectory>WebContent</warSourceDirectory> 
       <warName>HelloWebService</warName> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

[6]測試:

http://localhost:8080/HelloService/hello

http://localhost:8080/HelloService/hello?wsdl