Apache CXF . Apache . CXF .transport. HTTP . httpexception:



我正试图从axis迁移到cxf,因为它支持jax-rs,但这并不像我想象的那么容易。
我创建了一个示例web服务应用程序,并使用ANT将其部署到tomcat。当我检查$CATALINA_HOME/webapps/OrderProcessingSystem/WEB-INF时,一切似乎都很好。

-rw-r--r--. 1 root root  711 Oct 16 11:33 beans.xml
drwxr-xr-x. 3 root root 4096 Oct 16 11:34 classes
drwxr-xr-x. 2 root root 4096 Oct 16 12:53 lib
-rw-r--r--. 1 root root  787 Oct 16 12:36 web.xml
drwxr-xr-x. 2 root root    6 Oct 16 11:34 wsdl
我它指明:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" /> 
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 
    <jaxws:endpoint 
      id="orderProcess" 
      implementor="com.orderProcess.service.OrderProcessImpl" 
      address="/OrderProcess" />
</beans>

我的web . xml。

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE web-app PUBLIC 
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <display-name>CXF Servlet</display-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
</web-app>

客户端代码:

import com.orderProcess.service.Order;
import com.orderProcess.service.OrderProcess;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public final class Client {  
    public Client() {
    }
    public static void main(String args[]) throws Exception {
    // START SNIPPET: client
        ApplicationContext context = new ClassPathXmlApplicationContext("ClientBeans.xml");
        OrderProcess client = (OrderProcess) context.getBean("orderClient");
    // Populate the Order bean
        Order order = new Order();
        order.setCustomerID("C001");
        order.setItemID("I001");
        order.setQty(100);
        order.setPrice(200.00);
        String orderID = client.processOrder(order);
        String message = (orderID == null) ? "Order not approved" : "Order approved;order ID is " + orderID;
        System.out.println(message);
        System.exit(0);
    }
}

CLient beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://cxf.apache.org/jaxws
                           http://cxf.apache.org/schemas/jaxws.xsd">
    <jaxws:client 
        id="orderClient" 
        serviceClass="com.orderProcess.service.OrderProcess" 
        address="http://127.0.0.1:8080/OrderProcess" />
</beans>

我的ant构建(与extant构建的默认值相同,但做了一些小修改):

<?xml version="1.0"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements. See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership. The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License. You may obtain a copy of the License at
  http://www.apache.org/licenses/LICENSE-2.0
  Unless required by applicable law or agreed to in writing,
  software distributed under the License is distributed on an
  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  KIND, either express or implied. See the License for the
  specific language governing permissions and limitations
  under the License.
-->
<project name="Hello world demo" default="compile" basedir=".">
    <property environment="env"/>
    <property name="build.dir" location="${basedir}/build"/>
    <property name="build.classes.dir" location="${build.dir}/classes"/>
    <property name="src.dir" location="${basedir}/src"/>
    <property name="build.src.dir" location="${build.dir}/src"/>
    <property name="build.var.dir" location="${build.dir}/var"/>
    <property name="codegen.timestamp.file" value="${build.src.dir}/.CODEGEN_DONE"/>
    <property name="war.dir" location="${build.dir}/war"/>
    <property name="wsdl.dir" location="${basedir}/wsdl"/>
    <!-- Determine cxf.home, either from the environment variable CXF_HOME
        - or using ../..
        -->
    <condition property="cxf.home" value="${env.CXF_HOME}">
        <isset property="env.CXF_HOME"/>
    </condition>
    <property name="cxf.home" location="../.."/>
    <property name="cxf.etc.dir" location="${cxf.home}/etc"/>
    <!-- JAX-WS 2.2 and JAXB 2.2 require the API jars to be endorsed -->
    <condition property="cxf.endorsed.dir" value="${cxf.home}/lib/endorsed">
        <available file="${cxf.home}/lib/endorsed" type="dir" property=""/>
    </condition>
    <condition property="cxf.endorsed.flag" value="-Djava.endorsed.dirs=${cxf.endorsed.dir}">
        <available file="${cxf.home}/lib/endorsed" type="dir" property=""/>
    </condition>
    <property name="cxf.endorsed.dir" location="${cxf.home}"/>
    <property name="cxf.endorsed.flag" value="-Dnone=none"/>
    <!-- Set the classpath for the CXF libraries -->
    <path id="cxf.classpath">
        <pathelement location="${basedir}"/>
        <pathelement location="${build.classes.dir}"/>
        <pathelement path="${srcbuild.classpath}"/>
        <pathelement location="${cxf.home}/lib/cxf-manifest.jar"/>
        <fileset dir="${cxf.endorsed.dir}">
            <include name="*.jar"/>
        </fileset>
    </path>
    <!-- Sample macrodef to setup and run the CXF wsdl2java tool -->
    <macrodef name="wsdl2java">
        <attribute name="srcdestdir" default="${build.src.dir}"/>
        <attribute name="destdir" default="${build.classes.dir}"/>
        <attribute name="file"/>
        <attribute name="servicename.arg" default=""/>
        <attribute name="bindingfile" default=""/>
        <attribute name="databinding" default=""/>
        <attribute name="dir" default="${wsdl.dir}"/>
        <attribute name="package" default="NOT_SPECIFIED"/>
        <sequential>
            <mkdir dir="@{destdir}"/>
            <mkdir dir="@{srcdestdir}"/>
            <condition property="package.arg.@{file}" value="-p @{package}">
                <not>
                    <equals arg1="@{package}" arg2="NOT_SPECIFIED"/>
                </not>
            </condition>
            <property name="package.arg.@{file}" value=""/>
            <condition property="binding.arg" value="-b &quot;@{bindingfile}&quot;">
                <not>
                    <equals arg1="@{bindingfile}" arg2=""/>
                </not>
            </condition>
            <property name="binding.arg" value=""/>
            <condition property="databinding.arg" value="-db &quot;@{databinding}&quot;">
                <not>
                    <equals arg1="@{databinding}" arg2=""/>
                </not>
            </condition>
            <property name="databinding.arg" value=""/>
            <java failonerror="true" classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="yes">
                <classpath>
                    <path refid="cxf.classpath"/>
                </classpath>
                <jvmarg value="${cxf.endorsed.flag}"/>
                <sysproperty key="java.util.logging.config.file" value="${basedir}/logging.properties"/>
                <sysproperty key="exitOnFinish" value="true"/>
                <arg line="@{servicename.arg}"/>
                <arg line="${package.arg.@{file}}"/>
                <arg line="${databinding.arg}"/>
                <arg line="${binding.arg}"/>
                <arg value="-verbose"/>
                <arg value="-d"/>
                <arg value="@{srcdestdir}"/>
                <arg value="@{dir}/@{file}"/>
            </java>
        </sequential>
    </macrodef>
    <!-- Targets to run the sample client and server -->
    <target name="client" description="run demo client" depends="compile">
        <property name="param" value=""/>
        <cxfrun classname="com.orderProcessClient.service.Client"/>
    </target>
    <target name="server" description="run demo server" depends="compile">
        <cxfrun classname="com.orderProcess.service.OrderProcessImpl"/>
    </target>
    <target name="build"  depends="compile" description="build demo client and server"/>
    <!-- detect if the wsdl2java needs to be rerun -->
    <uptodate property="codegen.notrequired" value="true">
        <srcfiles dir="${wsdl.dir}" includes="**/*.wsdl"/>
        <srcfiles dir="${wsdl.dir}" includes="**/*.xsd"/>
        <mapper type="merge" to="${codegen.timestamp.file}"/>
    </uptodate>
    <target name="generate.code">
        <echo level="info" message="Generating code using wsdl2java..."/>
        <wsdl2java file="hello_world.wsdl"/>
    </target>
    <target name="maybe.generate.code" unless="codegen.notrequired">
        <antcall target="generate.code"/>
        <touch file="${codegen.timestamp.file}"/>
    </target>
    <!-- Compiles all the code.   Depends on the targets that would call wsdl2java -->
    <target name="compile" depends="maybe.generate.code">
        <mkdir dir="${build.classes.dir}"/>
        <mkdir dir="${build.src.dir}"/>
        <javac destdir="${build.classes.dir}" debug="true" fork="true" encoding="utf-8" includeantruntime="false">
            <compilerarg line="-J${cxf.endorsed.flag}"/>
            <src path="${src.dir}"/>
            <src path="${build.src.dir}"/>
            <classpath>
                <path refid="cxf.classpath"/>
                <path refid="spring.and.commons" />
                <path refid="cxf.extra.libs" />
            </classpath>
        </javac>
        <copy todir="${build.classes.dir}">
            <fileset dir="${src.dir}" excludes="**/*.java"/>
            <fileset dir="${build.src.dir}" includes="**/*.java"/>
        </copy>
    </target>
    <target name="clean" description="clean">
        <delete dir="${build.classes.dir}"/>
        <delete dir="${build.src.dir}"/>
        <delete file="${codegen.timestamp.file}"/>
        <delete file="demo.log"/>
        <delete dir="${build.var.dir}"/>
        <delete dir="${build.dir}"/>
    </target>
    <!-- Sample macrodef for running an application that would include the requirements that CXF needs
        It sets up the classpath and the endorsed dirs and add the logging properties and such -->
<path id="spring.and.commons">
    <fileset dir="/home/ramson/lib">
        <include name="*.jar" />
    </fileset>
</path>
<path id="cxf.extra.libs">
    <fileset dir="/usr/local/apache-cxf-3.1.4">
        <include name="*.jar" />
    </fileset>
</path>
    <macrodef name="cxfrun">
        <attribute name="logging-properties-file" default="${cxf.etc.dir}/logging.properties"/>
        <attribute name="classname"/>
        <attribute name="param1" default=""/>
        <attribute name="param2" default=""/>
        <attribute name="param3" default=""/>
        <attribute name="param4" default=""/>
        <attribute name="param5" default=""/>
        <attribute name="jvmarg1" default="-D' '"/>
        <attribute name="jvmarg2" default="-D' '"/>
        <attribute name="jvmarg3" default="-D' '"/>
        <attribute name="jvmarg4" default="-D' '"/>
        <attribute name="jvmarg5" default="-D' '"/>
        <sequential>
            <java classname="@{classname}" fork="yes">
                <classpath>
                    <path refid="cxf.classpath"/>
            <path refid="spring.and.commons" />
            <path refid="cxf.extra.libs" />
                </classpath>
                <arg value="@{param1}"/>
                <arg value="@{param2}"/>
                <arg value="@{param3}"/>
                <arg value="@{param4}"/>
                <arg value="@{param5}"/>
                <jvmarg value="-Dcxf.home=${cxf.home}"/>
                <jvmarg value="${cxf.endorsed.flag}"/>
                <jvmarg value="@{jvmarg1}"/>
                <jvmarg value="@{jvmarg2}"/>
                <jvmarg value="@{jvmarg3}"/>
                <jvmarg value="@{jvmarg4}"/>
                <jvmarg value="@{jvmarg5}"/>
                <sysproperty key="java.util.logging.config.file" value="@{logging-properties-file}"/>
            </java>
        </sequential>
    </macrodef>
    <macrodef name="wsdl2java">
        <attribute name="srcdestdir" default="${build.src.dir}"/>
        <attribute name="destdir" default="${build.classes.dir}"/>
        <attribute name="file"/>
        <attribute name="servicename.arg" default=""/>
        <attribute name="bindingfile" default=""/>
        <attribute name="dir" default="${wsdl.dir}"/>
        <attribute name="package" default="NOT_SPECIFIED"/>
        <sequential>
            <mkdir dir="@{destdir}"/>
            <mkdir dir="@{srcdestdir}"/>
            <condition property="package.arg.@{file}" value="-p @{package}">
                <not>
                    <equals arg1="@{package}" arg2="NOT_SPECIFIED"/>
                </not>
            </condition>
            <property name="package.arg.@{file}" value=""/>
            <condition property="binding.arg" value='-b "@{bindingfile}"'>
                <not>
                    <equals arg1="@{bindingfile}" arg2=""/>
                </not>
            </condition>
            <property name="binding.arg" value=""/>
            <java failonerror="true" classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="yes">
                <classpath>
                    <path refid="cxf.classpath" />
                </classpath>
                <sysproperty key="java.util.logging.config.file" value="${cxf.etc.dir}/logging.properties"/>
                <sysproperty key="exitOnFinish" value="true"/>
                <arg line="@{servicename.arg}"/>
                <arg line="${package.arg.@{file}}"/>
                <arg line="${binding.arg}"/>
                <arg value="-verbose"/>
                <arg value="-d"/>
                <arg value="@{srcdestdir}"/>
                <arg value="@{dir}/@{file}"/>
            </java>
        </sequential>
    </macrodef>
        <property name="war-lib" value="${basedir}/libs" />
        <property name="config.dir" value="${basedir}/config" />
    <macrodef name="cxfwar">
        <attribute name="filename"/>
        <attribute name="wsdl" default=""/>
        <attribute name="dir" default="${war.dir}"/>
        <attribute name="classesdir" default="${build.classes.dir}"/>
        <attribute name="webxml" default="${cxf.home}/etc/web.xml" />
        <sequential>
            <mkdir dir="@{dir}"/>
            <mkdir dir="${war-lib}"/>
            <mkdir dir="${config.dir}"/>
            <antcall target="copy-war-libs"/>
            <available property="has.config" file="${basedir}/webapp/WEB-INF" type="dir" />
            <antcall target="copy-config-files" />
            <delete file="@{dir}/@{filename}" />
            <war destfile="@{dir}/@{filename}" webxml="@{webxml}">
                <classes dir="@{classesdir}"/>
                <webinf dir="${wsdl.dir}">
                    <include name="cxf-servlet.xml"/>
                </webinf>
                <webinf dir="${wsdl.dir}/..">
                    <include name="wsdl/@{wsdl}"/>
                </webinf>
                <webinf dir="${config.dir}">
                        <include name="*.*" />
                </webinf> 
                <lib dir="${war-lib}">
                                <include name="*.jar"/>
                            </lib>
            </war>
            <delete dir="${war-lib}" />
            <delete dir="${config.dir}" />
        </sequential>
    </macrodef>
    <target name="copy-war-libs" unless="without.libs">
        <copy todir="${war-lib}">
            <fileset dir="${cxf.home}/lib">
               <exclude name="servlet-api-*.jar" />
                   <exclude name="geronimo-servlet_*.jar" />
                   <exclude name="jetty-*.jar"/>
                   <exclude name="WHICH_JARS" />
            </fileset>
        </copy> 
    </target>
    <target name="copy-config-files" if="has.config">
            <copy todir="${config.dir}" >
                <fileset dir="${basedir}/webapp/WEB-INF">
                    <include name="*.xml" />
                    <exclude name="web.xml" />
                </fileset>
            </copy>
    </target>
    <target name="deploy" description="deploy the application into the container">
        <antcall target="war"/>
        <antcall target="deploy-tomcat"/>
    </target>
    <target name="undeploy" depends="undeploy-tomcat" description="undeploy the application from the container"/>
    <target name="deploy-tomcat">
        <antcall target="validate-tomcat"/>
        <copy file="${war.dir}/${cxf.war.file.name}.war"
              todir="${env.CATALINA_HOME}/webapps"/>
    </target>
    <target name="undeploy-tomcat">
        <antcall target="validate-tomcat"/>
        <delete file="${env.CATALINA_HOME}/webapps/${cxf.war.file.name}.war"/>
        <delete dir="${env.CATALINA_HOME}/webapps/${cxf.war.file.name}"/>
    </target>
    <target name="validate-tomcat">
        <fail unless="env.CATALINA_HOME" message="You should set the CATALINA_HOME, if you want to deploy into tomcat"/>
    </target>
</project>

现在,这是我的痛苦:当我运行客户端

ant client

我得到这个错误:

Buildfile: /home/ramson/NetBeansProjects/OrderProcessingSystem/masterbuild/build.xml
Trying to override old definition of task wsdl2java
maybe.generate.code:
compile:
build:
client:
     [java] Exception in thread "main" javax.xml.ws.WebServiceException: Could not send Message.
     [java]     at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:145)
     [java]     at com.sun.proxy.$Proxy39.processOrder(Unknown Source)
     [java]     at com.orderProcessClient.service.Client.main(Client.java:32)
     [java] Caused by: org.apache.cxf.transport.http.HTTPException: HTTP response '404: Not Found' when communicating with http://127.0.0.1:8080/OrderProcess
     [java]     at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1526)
     [java]     at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1486)
     [java]     at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1305)
     [java]     at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
     [java]     at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:623)
     [java]     at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
     [java]     at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:271)
     [java]     at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:541)
     [java]     at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:474)
     [java]     at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:377)
     [java]     at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:330)
     [java]     at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
     [java]     at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:134)
     [java]     ... 2 more
     [java] Java Result: 1
BUILD SUCCESSFUL
Total time: 2 seconds

最初我认为如果我在url中包含战争名称可能会有区别,但是没有。

address="http://127.0.0.1:8080/OrderProcess"

address="http://127.0.0.1:8080/OrderProcessingSystem/OrderProcess"

我哪里错了?

在您的"web.xml"中,您在以下URL模式上定义了CXFServlet: "/services/*"。您的端点发布在"/OrderProcess"上。

因此,你在客户端的服务地址需要是:"http://127.0.0.1:8080/services/OrderProcess"。

根据您的部署方式,您可能需要包括前面提到的WAR名称。在这种情况下,地址变成:"http://127.0.0.1:8080/OrderProcessingSystem/services/OrderProcess"。

相关内容

  • 没有找到相关文章

最新更新