学习CXF WebService入门实例一

更新时间:2024-06-01 00:19:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

最近开发开始学习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地址栏在哪里,对不起,大哥,地球是危险的,你回火星去吧!如果正常的话

呢,大约在网页上会出现下面的信息,只截取一部分,注意画粗红线的地方,下面我们用的到。

五、客户端类(http://hello.ws.com/,这个就是上面画粗红线的地方) package com.ws.client; import java.net.URL;

import javax.xml.namespace.QName; import javax.xml.ws.Service;

import javax.xml.ws.soap.SOAPBinding;

import com.ws.entity.Users;

import com.ws.hello.HelloWorldImpl; import com.ws.hello.IHelloWorld;

public final class Client {

private static final QName SERVICE_NAME = new QName(\, \); private static final QName PORT_NAME = new QName(\, \);

public static void main(String args[]) throws Exception { Service service = Service.create(SERVICE_NAME);

String endpointAddress = \

private Client() { }

service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);

IHelloWorld hw = service.getPort(IHelloWorld.class); System.out.println(hw.sayHello(\张述飞\

} }

这样写点击运行的话,不知道为什么会报异常,如果有不报异常的话,请贴出来,谢谢! Exception in thread \javax.xml.ws.WebServiceException: WSDL Metadata not available to create the proxy, either Service instance or ServiceEndpointInterface com.ws.hello.IHelloWorld should have WSDL information

at

com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)

at

com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(Unknown Source)

at javax.xml.ws.Service.getPort(Service.java:168) at com.ws.client.Client.main(Client.java:29)

/**

URL url = new URL(\

Service service = Service.create(url,SERVICE_NAME);

IHelloWorld hw = service.getPort(IHelloWorld.class); System.out.println(hw.sayHello(\张述飞\

System.out.println(hw.getUserName(new Users(1,\ for(Users user : hw.getListUser()) { System.out.println(\ }*/

[id:\

将上面紫色代码注释,运行蓝色代码正常!这是小白我学习webservice的第一个程序,不知道为什么都是坑啊,一步一个坑,难道是坑坑更健康?将代码贴出来,希望有用到的或者要学习的小白们能少走一段弯路!本来想把工程给免费发布出来,后来想想还是算了吧,小白们还是手把手的配置一下吧!

本文来源:https://www.bwwdw.com/article/0bs6.html

Top