学习CXF WebService入门实例一

2019-04-16 21:19

最近开发开始学习Web Service,如果你是大神,请路过!谢谢!遵循前辈大神们的教导~~~,内事不决问度娘,外事不决问谷歌(现在谷歌已经不能用了),只能问度娘了!上网一百度,套用周董的一句歌词,霍,霍,霍,霍,这么多的套路(axis,axis2,XFire,CXF等),我到底选择哪一个?因为要和Spring进行对接,看了一下,CXF与Spring的耦合度最好,于是就选择了CXF。上官网下jar包,下了最新的apache-cxf-3.1.4.zip包。解压出来,看看里面的最简单的实例,apache-cxf-3.1.4\\samples\\java_first_jaxws,本着你快乐所以我快乐加上不要脸的原则,我抄抄抄,改了一下名字,开发环境jdk1.6, jdk1.5没试过,上眼呐! 新建工程mywbs,导入jar包

cxf-core-3.1.4.jar

jetty-continuation-9.2.11.v20150529.jar jetty-http-9.2.11.v20150529.jar jetty-io-9.2.11.v20150529.jar jetty-server-9.2.11.v20150529.jar jetty-util-9.2.11.v20150529.jar wsdl4j-1.6.3.jar

xmlschema-core-2.2.1.jar

一、接口类IHelloWorld.java

package com.ws.hello; import java.util.List; import javax.jws.WebService; import com.ws.entity.Users; @WebService

public interface IHelloWorld { }

public String sayHello(String name); public String getUserName(Users user); public List getListUser();

二、实现类(说明endpointInterface = \com.ws.hello.IHelloWorld\,IHelloWorld类加上路径,此处注意)HelloWorldImpl.java

package com.ws.hello; import java.util.ArrayList; import java.util.List; import javax.jws.WebService; import com.ws.entity.Users;

@WebService(endpointInterface = \com.ws.hello.IHelloWorld\,serviceName = \, portName=\)

public class HelloWorldImpl implements IHelloWorld {

@Override

public String sayHello(String name) {

return name + \您好啊!\;

}

}

@Override

public String getUserName(Users user) { }

@Override

public List getListUser() { }

System.out.println(\); List list = new ArrayList(); list.add(new Users(2,\张三\)); list.add(new Users(3, \十八罗汉\)); list.add(new Users(4,\五王\)); return list;

return user.getName();

三、实体类Users

package com.ws.entity; import java.io.Serializable;

public class Users implements Serializable{

private static final long serialVersionUID = -5031894017095689998L; private Integer id; private String name; public Integer getId() { }

public void setId(Integer id) { }

public String getName() { }

public void setName(String name) { }

public Users(Integer id, String name) { }

public Users() {

super(); this.id = id; this.name = name; this.name = name; return name; this.id = id; return id;

}

}

super();

Users类如果不写默认构造方法,将报如下异常:

Exception in thread \javax.xml.ws.WebServiceException: Unable to create JAXBContext

at

com.sun.xml.internal.ws.model.AbstractSEIModelImpl.createJAXBContext(Unknown Source)

at

com.sun.xml.internal.ws.model.AbstractSEIModelImpl.postProcess(Unknown Source)

at

com.sun.xml.internal.ws.model.RuntimeModeler.buildRuntimeModel(Unknown Source)

at

com.sun.xml.internal.ws.server.EndpointFactory.createSEIModel(Unknown Source)

at

com.sun.xml.internal.ws.server.EndpointFactory.createEndpoint(Unknown Source)

at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown at com.sun.xml.internal.ws.api.server.WSEndpoint.create(Unknown at Source) Source)

com.sun.xml.internal.ws.transport.http.server.EndpointImpl.createEndpoint(Unknown Source)

at

com.sun.xml.internal.ws.transport.http.server.EndpointImpl.publish(Unknown Source)

at

com.sun.xml.internal.ws.spi.ProviderImpl.createAndPublishEndpoint(Unknown Source)

at javax.xml.ws.Endpoint.publish(Endpoint.java:220) at

com.ws.deploy.DeployHelloWorldService.(DeployHelloWorldService.java:14)

at

com.ws.deploy.DeployHelloWorldService.main(DeployHelloWorldService.java:23)

Caused by: java.security.PrivilegedActionException:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions

com.ws.entity.Users does not have a no-arg default constructor.

at java.security.AccessController.doPrivileged(Native Method) ... 13 more

this problem is related to the following location:

at com.ws.entity.Users at public java.util.List

at com.ws.hello.jaxws.GetListUserResponse

com.ws.hello.jaxws.GetListUserResponse._return

Caused by:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions

com.ws.entity.Users does not have a no-arg default constructor.

at

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)

at

com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)

at

com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.(Unknown Source)

at

com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)

at

com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)

at com.sun.xml.internal.bind.api.JAXBRIContext.newInstance(Unknown at Source)

com.sun.xml.internal.ws.developer.JAXBContextFactory$1.createJAXBContext(Unknown Source)

at

com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown

this problem is related to the following location:

at com.ws.entity.Users at public java.util.List

at com.ws.hello.jaxws.GetListUserResponse

com.ws.hello.jaxws.GetListUserResponse._return

Source)

at

com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown Source)

... 14 more

晕了,真是崩溃了,就写了这么几句话!错误代码比正常代码还要多!真是婶婶能忍叔叔不能忍,虽然英语很烂,连猜带蒙吧,看黑色的一句话,大概似乎是告诉我们Users类中没有默认的构造方法,果断加上,错误消失!此处应有掌声!鼓掌!!!

四、DeployHelloWorldService.java

package com.ws.deploy;

import javax.xml.ws.Endpoint; import com.ws.hello.HelloWorldImpl;

public class DeployHelloWorldService {

protected DeployHelloWorldService() throws Exception { System.out.println(\);

HelloWorldImpl implementor = new HelloWorldImpl(); //工程名

String address = \; Endpoint.publish(address, implementor); }

/**

* @param args

* @throws Exception */

public static void main(String[] args) throws Exception {

new DeployHelloWorldService();

System.out.println(\); Thread.sleep(5 * 60 * 1000);

System.out.println(\); System.exit(0); }

}

点击运行,正常的话会有

Starting Server Server ready...

在IE地址栏中输入:http://localhost:8080/mywbs?wsdl

什么你不知道IE地址栏在哪里,对不起,大哥,地球是危险的,你回火星去吧!如果正常的话


学习CXF WebService入门实例一.doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2014年辽宁省普通高中地理学业水平考试测试题及答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: