A Smple JAX-WS Service in the CXF Distribution

来源:互联网 发布:苹果mac死机 编辑:程序博客网 时间:2024/05/24 02:41

Writing your Service 1

Wirting your service interface at first 1

Implement your service inerface secondly 1

Writing your service at thirdly 2

Testing your service at last 2

Accessing your service by client 2

 

Writing your Service

Wirting your service interface at first 

1.申明服务提供的接口函数,数量不唯一。

2.做必要的注释

import javax.jws.WebService;

import javax.xml.ws.BindingType;

 

@WebService

@BindingType(value = javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING)

public interface HelloWorld{

String SayHello_TO_Client();

//Add other operations your need

}

Implement your service inerface secondly

1.实现服务提供的接口函数。

@WebService(endpointInterface = "demo.hw.server.HelloWorld",

            serviceName = "HelloWorld")    //注解命名空间和服务名称

public class HelloWorldImpl implements HelloWorld {

@Override

public String SayHello_TO_Client() {

String str =say hello to client

return str;

}

//Add other operations implement your add in interface

}

Writing your service at thirdly

Method-1

public class Service {

 

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

    HelloWorldImpl helloworldImpl = new HelloWorldImpl();

JaxWsServerFactoryBean factory2 = new JaxWsServerFactoryBean(); factory2.setServiceClass(HelloWorld.class);

factory2.setAddress("http://localhost:9000/helloWorld?wsdl");

//服务地址构成 http://IP:端口号/接口名称(可选)?wsdl

factory2.setServiceBean(helloworldImpl);

factory2.create();

    }  

Testing your service at last

Pointing your browser at http://localhost:9000/helloWorld?wsdl will display the wsdl for this service

Accessing your service by client

public static String GetServerStr() {

SoapObject soapObject = new SoapObject("http://unknown.namespace/""SayHello_TO_Client()");

//soapObject.addProperty("byProvinceName", byProvinceName);  如果需要传入参数则通过此方法

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //版本

envelope.bodyOut = soapObject;

envelope.dotNet = false;

envelope.setOutputSoapObject(soapObject);

try {

HttpTransportSE trans = new HttpTransportSE("http://localhost:9000/helloWorld?wsdl");

trans.debug = false// 调试功能

trans.call(null, envelope);

catch (IOException e) {

e.printStackTrace();

return null;

catch (XmlPullParserException e) {

e.printStackTrace();

return null;

}

str = ((SoapObject) envelope.bodyIn).getProperty("return").toString();

return str;

}