最近开发开始学习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

publicinterface IHelloWorld {

    public String sayHello(String name);

    public String getUserName(Users user);

    public List<Users> 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 ="IHelloWorld", portName="IHelloWorldPort")

publicclassHelloWorldImpl implements IHelloWorld {

 

    @Override

    public String sayHello(String name) {

       return name +": 您好啊!";

    }

   

    @Override

    public String getUserName(Users user) {

       return user.getName();     

    }

   

    @Override

    public List<Users> getListUser() {

       System.out.println("getListUser called~~~");

       List<Users> list = new ArrayList<Users>();

       list.add(new Users(2,"张三"));

       list.add(new Users(3,"十八罗汉"));

       list.add(new Users(4,"五王"));

       return list;

    }

 

}

 

三、实体类Users

package com.ws.entity;

import java.io.Serializable;

publicclass Usersimplements Serializable{

    privatestaticfinallongserialVersionUID = -5031894017095689998L;

    private Integerid;

    private Stringname;

    public Integer getId() {

       returnid;

    }

    publicvoid setId(Integer id) {

       this.id = id;

    }

    public String getName() {

       returnname;

    }

    publicvoid setName(String name) {

       this.name = name;

    }

    public Users(Integer id, String name) {

       super();

       this.id = id;

       this.name = name;

    }

    public Users() {

       super();

    }

}

 

 

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

Exception in thread "main" 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 Source)

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

    at 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.<init>(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.

    this problem is related to the following location:

       at com.ws.entity.Users

       at public java.util.List com.ws.hello.jaxws.GetListUserResponse._return

       at com.ws.hello.jaxws.GetListUserResponse

 

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

    ... 13 more

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.

    this problem is related to the following location:

       at com.ws.entity.Users

       at public java.util.List com.ws.hello.jaxws.GetListUserResponse._return

       at com.ws.hello.jaxws.GetListUserResponse

 

    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.<init>(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 Source)

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

    at com.sun.xml.internal.ws.model.AbstractSEIModelImpl$1.run(Unknown 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;

 

publicclass DeployHelloWorldService {

    protected DeployHelloWorldService()throws Exception {

        System.out.println("Starting Server");

        HelloWorldImpl implementor =new HelloWorldImpl();

       //工程名

        String address = "http://localhost:8080/mywbs";

        Endpoint.publish(address, implementor);

    }

   

    /**

     * @param args

     * @throws Exception

     */

    publicstaticvoid main(String[] args)throws Exception {

        new DeployHelloWorldService();

        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);

        System.out.println("Server exiting");

        System.exit(0);

    }

 

}

 

点击运行,正常的话会有

Starting Server

Server ready...

 

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

什么你不知道IE地址栏在哪里,对不起,大哥,地球是危险的,你回火星去吧!如果正常的话呢,大约在网页上会出现下面的信息,只截取一部分,注意画粗红线的地方,下面我们用的到。

 CXF Web Service入门实例一_WebService

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

package com.ws.client;

importjava.net.URL;

 

import javax.xml.namespace.QName;

import javax.xml.ws.Service;

import javax.xml.ws.soap.SOAPBinding;

 

importcom.ws.entity.Users;

importcom.ws.hello.HelloWorldImpl;

import com.ws.hello.IHelloWorld;

 

publicfinalclass Client {

    privatestaticfinal QNameSERVICE_NAME =new QName("http://hello.ws.com/","IHelloWorld");

    privatestaticfinal QNamePORT_NAME =new QName("http://hello.ws.com/","IHelloWorldPort");

 

 

    private Client() {

      

    }

 

    publicstaticvoid main(String args[])throws Exception {

        Service service = Service.create(SERVICE_NAME);

        String endpointAddress = "http://localhost:8080/mywbs";

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

       

        IHelloWorld hw = service.getPort(IHelloWorld.class);

        System.out.println(hw.sayHello("张述飞"));

 

       /**

       URL url = new URL("http://localhost:8080/mywbs?wsdl");

        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,"kaka")));

        for(Users user : hw.getListUser()) {

        System.out.println("List User [id:"+user.getId()+"][name:"+user.getName()+"]");

        }*/

 

}

}

 

这样写点击运行的话,不知道为什么会报异常,如果有不报异常的话,请贴出来,谢谢!

Exception in thread "main" 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)

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