WebService - tomat / eclipse / maven - 请求的资源不可用

时间:2016-10-10 20:26:24

标签: eclipse maven tomcat

我正在尝试使用Eclipse和Maven在Tomcat上运行一个简单的Web服务。

我继续收到此消息: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 / sun-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中我通常会这样做(例如对于Spring MVC):

[1]项目&gt;清洁 [2]运行为&gt; Maven干净 [3]运行为&gt; Maven安装 [4]运行为&gt;在服务器上运行

我应该使用以下网址进行测试: http://localhost:8080/HelloWebService/helloWebService

没有错误。没有堆栈跟踪。只有404,没有资源可用。通过Tomcat管理器,我看到该服务正在运行。

1 个答案:

答案 0 :(得分:0)

通过另一个网站,我看到了正确的实现。 @RITZ XAVI - 感谢您指向我的文件夹结构。我将web.xml放在WebContents(而不是webapp)下。

正确的最终解决方案是:

[1] Webservice接口:

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

[2] Webservice实施:

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

[3] sun-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