博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CXF框架的一些问题
阅读量:7033 次
发布时间:2019-06-28

本文共 13265 字,大约阅读时间需要 44 分钟。

hot3.png

  1. 去官网下载CXF框架(我当期的版本是apache-cxf-3.0.15),
  2. 配置环境变量:①CXF_HOME=D:\javasoftware\apache-cxf-3.0.15;
                            ②在path后面加上 %CXF_HOME%/bin;
  3. cmd控制台:wsdl2java -p com.haid.webservice.client.bpm -d E:\haid\project\warrent_estimate\cxf -client http://10.16.8.207:8010/Portal/WebServices/EffortService.asmx?wsdl

 

    附wsdl2java用法:

    wsdl2java -p com -d D:\\src -all xx.wsdl

    -p 指定其wsdl的命名空间,也就是要生成代码的包名:

    -d 指定要产生代码所在目录

    -client 生成客户端测试web service的代码

    -server 生成服务器启动web service的代码

    -impl 生成web service的实现代码

    -ant 生成build.xml文件

    -all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline,     implementation object, and an Ant build.xml file.

 

-------------------------------------------------------------------------------------------------------------

package com.haid.webservice.client.bpm;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "", propOrder = {    "workerCode",    "workerName"})@XmlRootElement(name = "GetOTUser")public class GetOTUser {    @XmlElement(name = "WorkerCode")    protected String workerCode;    @XmlElement(name = "WorkerName")    protected String workerName;    public String getWorkerCode() {        return workerCode;    }    public void setWorkerCode(String value) {        this.workerCode = value;    }    public String getWorkerName() {        return workerName;    }    public void setWorkerName(String value) {        this.workerName = value;    }}

 

package com.haid.webservice.client.bpm;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlType;@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "", propOrder = {    "getOTUserResult"})@XmlRootElement(name = "GetOTUserResponse")public class GetOTUserResponse {    @XmlElement(name = "GetOTUserResult")    protected String getOTUserResult;    public String getGetOTUserResult() {        return getOTUserResult;    }    public void setGetOTUserResult(String value) {        this.getOTUserResult = value;    }}

 

package com.haid.webservice.client.bpm;import javax.xml.bind.JAXBElement;import javax.xml.bind.annotation.XmlElementDecl;import javax.xml.bind.annotation.XmlRegistry;import javax.xml.namespace.QName;@XmlRegistrypublic class ObjectFactory {    private final static QName _String_QNAME = new QName("http://tempuri.org/", "string");    private final static QName _Boolean_QNAME = new QName("http://tempuri.org/", "boolean");    private final static QName _ArrayOfStaffInfo_QNAME = new QName("http://tempuri.org/", "ArrayOfStaffInfo");classes for package: com.haid.webservice.client.bpm    public ObjectFactory() {    }    public GetOTUser createGetOTUser() {        return new GetOTUser();    }    public GetOTUserResponse createGetOTUserResponse() {        return new GetOTUserResponse();    }    @XmlElementDecl(namespace = "http://tempuri.org/", name = "string")    public JAXBElement
createString(String value) { return new JAXBElement
(_String_QNAME, String.class, null, value); } @XmlElementDecl(namespace = "http://tempuri.org/", name = "boolean") public JAXBElement
createBoolean(Boolean value) { return new JAXBElement
(_Boolean_QNAME, Boolean.class, null, value); }}

 

这个是用于封装xml的报文请求消息。改动这里,会改变xml的结构。下面将会测试具体的例子。

package com.haid.webservice.client.bpm;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.bind.annotation.XmlSeeAlso;import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.882+08:00 * Generated source version: 3.0.15 *  */@WebService(targetNamespace = "http://tempuri.org/", name = "EffortServiceSoap")@XmlSeeAlso({ObjectFactory.class})public interface EffortServiceSoap {    @WebResult(name = "GetOTUserResult", targetNamespace = "http://tempuri.org/")    @RequestWrapper(localName = "GetOTUser", targetNamespace = "http://tempuri.org/", className = "com.haid.webservice.client.bpm.GetOTUser")    @WebMethod(operationName = "GetOTUser", action = "http://tempuri.org/GetOTUser")className = "com.haid.webservice.client.bpm.GetOTUserResponse")    @ResponseWrapper(localName = "GetOTUserResponse", targetNamespace = "http://tempuri.org/", className = "com.haid.webservice.client.bpm.GetOTUserResponse")    public java.lang.String getOTUser(        @WebParam(name = "WorkerCode"/*, targetNamespace = "http://tempuri.org/"*/)        java.lang.String workerCode,        @WebParam(name = "WorkerName"/*, targetNamespace = "http://tempuri.org/"*/)        java.lang.String workerName    );}

 

这个是测试执行的main方法:

package com.haid.webservice.client.bpm;/** * Please modify this class to meet your needs * This class is not complete */import java.io.File;import java.net.MalformedURLException;import java.net.URL;import javax.xml.namespace.QName;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.bind.annotation.XmlSeeAlso;import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.824+08:00 * Generated source version: 3.0.15 *  */public final class EffortServiceSoap_EffortServiceSoap_Client {    private static final QName SERVICE_NAME = new QName("http://tempuri.org/", "EffortService");    private EffortServiceSoap_EffortServiceSoap_Client() {    }    public static void main(String args[]) throws java.lang.Exception {        URL wsdlURL = EffortService.WSDL_LOCATION;        if (args.length > 0 && args[0] != null && !"".equals(args[0])) {             File wsdlFile = new File(args[0]);            try {                if (wsdlFile.exists()) {                    wsdlURL = wsdlFile.toURI().toURL();                } else {                    wsdlURL = new URL(args[0]);                }            } catch (MalformedURLException e) {                e.printStackTrace();            }        }              EffortService ss = new EffortService(wsdlURL, SERVICE_NAME);        EffortServiceSoap port = ss.getEffortServiceSoap();                  {        System.out.println("Invoking getOTUser...");        java.lang.String _getOTUser_workerCode = "";        java.lang.String _getOTUser_workerName = "吴";        java.lang.String _getOTUser__return = port.getOTUser(_getOTUser_workerCode, _getOTUser_workerName);        System.out.println("getOTUser.result=" + _getOTUser__return);        }        System.exit(0);    }}

 

运行main方法,结构是直接报错了,报错信息如下:

Caused by: javax.xml.bind.UnmarshalException

 - with linked exception:
[com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 266; 意外的元素 (uri:"http://tempuri.org/", local:"GetOTUserResult")。所需元素为<{}GetOTUserResult>]
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:483)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:417)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:394)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.doUnmarshal(JAXBEncoderDecoder.java:843)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.access$100(JAXBEncoderDecoder.java:102)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder$2.run(JAXBEncoderDecoder.java:871)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:869)
    at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:703)
    at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:160)
    at org.apache.cxf.wsdl.interceptors.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:108)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
    at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:798)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1638)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1527)
    at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1330)
    at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
    at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:638)
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
    at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:423)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:326)
    at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:279)
    at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:138)
    ... 2 more
Caused by: com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 266; 意外的元素 (uri:"http://tempuri.org/", local:"GetOTUserResult")。所需元素为<{}GetOTUserResult>
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:693)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:262)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:257)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:124)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:105)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:262)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:525)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:507)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:246)
    at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:180)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
    ... 26 more
Caused by: javax.xml.bind.UnmarshalException: 意外的元素 (uri:"http://tempuri.org/", local:"GetOTUserResult")。所需元素为<{}GetOTUserResult>
    ... 37 more

其实,我也不知道是啥原因导致的,我就是在不停的尝试修改EffortServiceSoap 类的配置信息。能够正确获取服务器数据的配置是如下样子:

package com.haid.webservice.client.bpm;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.bind.annotation.XmlSeeAlso;import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.882+08:00 * Generated source version: 3.0.15 *  */@WebService(targetNamespace = "http://tempuri.org/", name = "EffortServiceSoap")@XmlSeeAlso({ObjectFactory.class})public interface EffortServiceSoap {    @WebResult(name = "GetOTUserResult", targetNamespace = "http://tempuri.org/")    @WebMethod(operationName = "GetOTUser", action = "http://tempuri.org/GetOTUser")className = "com.haid.webservice.client.bpm.GetOTUserResponse")    public java.lang.String getOTUser(        @WebParam(name = "WorkerCode", targetNamespace = "http://tempuri.org/")        java.lang.String workerCode,        @WebParam(name = "WorkerName", targetNamespace = "http://tempuri.org/")        java.lang.String workerName    );}

报文信息如下:

 

如果我改成这样子:

package com.haid.webservice.client.bpm;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;import javax.xml.bind.annotation.XmlSeeAlso;import javax.xml.ws.RequestWrapper;import javax.xml.ws.ResponseWrapper;/** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.882+08:00 * Generated source version: 3.0.15 *  */@WebService(targetNamespace = "http://tempuri.org/", name = "EffortServiceSoap")@XmlSeeAlso({ObjectFactory.class})public interface EffortServiceSoap {    @WebResult(name = "GetOTUserResult", targetNamespace = "http://tempuri.org/")    @RequestWrapper(localName = "GetOTUser", targetNamespace = "http://tempuri.org/", className = "com.haid.webservice.client.bpm.GetOTUser")    @WebMethod(operationName = "GetOTUser", action = "http://tempuri.org/GetOTUser")className = "com.haid.webservice.client.bpm.GetOTUserResponse")    public java.lang.String getOTUser(        @WebParam(name = "WorkerCode"/*, targetNamespace = "http://tempuri.org/"*/)        java.lang.String workerCode,        @WebParam(name = "WorkerName"/*, targetNamespace = "http://tempuri.org/"*/)        java.lang.String workerName    );}

服务器那边是解析不到不到参数的。传输的请求报文信息如下:

'吴'

 

转载于:https://my.oschina.net/u/2381372/blog/1548320

你可能感兴趣的文章
[FJOI2016]建筑师
查看>>
程序猿崛起——Growth Hacker
查看>>
设置SecureCRT会话的缓冲区大小
查看>>
C语言中的强符号与弱符号
查看>>
java中intern().
查看>>
这篇讲PHP的讲的有些道理 & mb_substr & 中文处理
查看>>
XMPPFramework 下载运行报错:@import libxmlSimu
查看>>
在vs2005项目中,将.sln文件和.suo文件放在一个独立的文件夹内
查看>>
alias命令别名
查看>>
day2 列表中常用的方法
查看>>
VMware 虚拟化编程(3) —VMware vSphere Web Service API 解析
查看>>
2017.7.12
查看>>
应该记住的 30 个 CSS 选择器
查看>>
[MedicalEndoscope]Monitor Resolution Summarize
查看>>
将方法绑定到委托
查看>>
Trees on the level (二叉链表树)
查看>>
Android系统启动过程学习
查看>>
openstack的第二天
查看>>
Windows 10 安装MySQL 8.0.11
查看>>
字节对齐概要
查看>>