使用JAX-WS standard Endpoint APIs开发WebService完整的例子

34 篇文章 0 订阅
编程发布WebService方式的完整例子
WS服务端:
(1)HelloWorld.java接口
package com.xxx.ws.code.server;  

import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHi(String text);
}


(2)实现类HelloWorldImpl.java
package com.xxx.ws.code.server.impl;  

import javax.jws.WebService;

import com.xxx.ws.code.server.HelloWorld;

@WebService(endpointInterface = "com.xxx.ws.code.server.HelloWorld",
serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {

public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}

}


(3)发布web service
package com.xxx.ws.code.server;  

//import javax.xml.ws.Endpoint;

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

import com.xxx.ws.code.server.impl.HelloWorldImpl;

public class RunCXFServer {

protected RunCXFServer() throws Exception {
// START SNIPPET: publish
System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:8002/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
svrFactory.create();

// END SNIPPET: publish
}

public static void main(String args[]) throws Exception {
new RunCXFServer();
System.out.println("Server ready...");
}
}


(4)运行RunCXFServer
http://localhost:8002/helloWorld?wsdl就可以看到输出的saop envelope了.

编写客户端调用WebService
package com.xxx.ws.code.client;  

//import javax.xml.namespace.QName;
//import javax.xml.ws.Service;
//import javax.xml.ws.soap.SOAPBinding;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;

import com.xxx.ws.code.server.HelloWorld;

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

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:8002/helloWorld");
HelloWorld client = (HelloWorld) factory.create();

String reply = client.sayHi("HI");
System.out.println("Server said: " + reply);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值