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

标签 eclipse maven tomcat

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

我不断收到此消息:404 请求的资源不可用。

服务器运行良好,没有错误信息。在主机管理器中,网站显示为正在运行。单击它会出现... 404。

Controller 是:

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] 项目 > 清理 [2] 运行方式 > Maven 清理 [3] 运行方式 > Maven 安装 [4] 运行方式 > 在服务器上运行

我应该使用以下 URL 对其进行测试: http://localhost:8080/HelloWebService/helloWebService

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

最佳答案

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

最终正确的解决方案是:

[1] 网络服务接口(interface):

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

[2] 网络服务实现:

@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

关于eclipse - WebService - tomat/eclipse/maven - 请求的资源不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39966202/

相关文章:

java - GWT-Maven-插件 : Link GWT compiled files into a defined directory

java - 如何在1个web.xml中定义2个servlet

java - GoogleApps - 如何导入我的应用程序

maven - 无法在项目上执行目标 org.wildfly.plugins :wildfly-maven-plugin:1. 0.0.Final:deploy (default-cli)

Eclipse/CFBuilder - 跟踪更改的文件。

java - 安装带有依赖的jar到maven仓库(Android gcm-server push library)

java - 从在 Tomcat 中运行的 Web 应用程序写入 Socket 时出现 NullPointerException

tomcat - WS02 DeviceMGT 到 Tomcat 服务器

java - Eclipse 从构建路径中删除测试文件夹

java - Eclipse 自发地禁用事件突出显示