0

我知道这是 jax-ws Web 服务的非常基本的示例,但我无法解决此错误。我正在关注本教程http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/ 并使用 ant 使用 build.xml 制作了战争文件。但是当我将此war文件复制粘贴到apache tomcat的webapps目录时,它会被部署,但是这个url localhost:8084/HelloWorld/hello 给出了错误404(我有apache tomcat在端口号8084上运行)。我在下面包含了war文件的内容:

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>

太阳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="HelloWorld"
      implementation="com.mkyong.ws.HelloWorldImpl"
      url-pattern="/hello"/>
</endpoints>

构建.xml

<project name="HelloWorld" default="dist" basedir=".">
    <description>
        Web Services build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>
  <property name="webcontent"  location="WebContent"/>

  <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init"
    description="compile the source " >
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="war" depends="compile"
    description="generate the distribution war" >

    <!-- Create the war distribution directory -->
    <mkdir dir="${dist}/war"/>

    <!-- Follow standard WAR structure -->
    <copydir dest="${dist}/war/build/WEB-INF/" src="${webcontent}/WEB-INF/" />
    <copydir dest="${dist}/war/build/WEB-INF/classes/" src="${build}" />

    <jar jarfile="${dist}/war/HelloWorld-${DSTAMP}.war" basedir="${dist}/war/build/"/>
  </target>

</project>

HelloWorld.java

package com.mkyong.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{

    @WebMethod String getHelloWorldAsString();

}

HelloWorldImpl.java

package com.mkyong.ws;

import javax.jws.WebService;

//Service Implementation Bean

@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{

    @Override
    public String getHelloWorldAsString() {
        return "Hello World JAX-WS";
    }
}

请指出我哪里出错了

编辑:我在 Tomcat 日志中遇到以下异常:

SEVERE: ContainerBase.addChild: start: 
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1105)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1664)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContainerInitializer
    at java.lang.ClassLoader.findBootstrapClass(Native Method)
    at java.lang.ClassLoader.findBootstrapClass0(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1629)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1655)
    at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1565)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1273)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:878)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:369)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5173)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 11 more
Nov 4, 2012 5:25:35 PM org.apache.catalina.startup.HostConfig deployDirectory
SEVERE: Error deploying web application directory F:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\HelloWorld
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/HelloWorld]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1105)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1664)
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

2 回答 2

3

由于程序所需的库版本与 JDK 默认随附的版本之间存在差异,您会遇到此错误。您正在运行 JAX-WS 2.2(至少需要 2.2 的 JAXB 版本)Web 服务示例,但您的 JDK 6 和您的 tomcat 安装附带 JAXB 2.1 版,导致您遇到异常。

要解决此问题,请使用 JDK认可的标准覆盖机制,该机制实质上允许您使用较新版本的相同版本覆盖作为 JDK 普通安装的一部分打包的默认库/jar。

  1. 下载JAXB apiJAX-WS库的最新副本并放置在<JDK_HOME>\lib\endorsed路径中,其中<JDK_HOME>指的是 JDK 的安装文件夹(我假设这里是 windows)。如果该endorsed文件夹不存在于该路径中,请创建它并将新副本放在那里。

  2. 将相同的下载文件放在<TOMCAT_HOME>\endorsed\您的 Tomcat 安装路径中。和以前一样,如果endorsed不存在,则创建它。

你可以走了

于 2012-10-31T14:59:40.640 回答
0

如果您使用的是 JDK 6 或更低版本。升级它。我遇到了同样的问题,在将 JDK serion 升级到 7 之后它就消失了。

于 2016-03-03T12:32:55.133 回答