服务接口及实现类很简单:
接口 com.service.StudentService:
03 |
import javax.jws.WebParam;
|
05 |
import javax.jws.WebService;
|
09 |
public interface StudentService {
|
11 |
String helloStudent( @WebParam (name= "text" )String name);
|
实现类 com.service.impl.StudentServiceImpl:
01 |
package com.service.impl;
|
03 |
import javax.jws.WebService;
|
05 |
import com.service.StudentService;
|
07 |
@WebService (endpointInterface= "com.service.StudentService" ,targetNamespace= "http://service.com/" )
|
09 |
public class StudentServiceImpl implements StudentService{
|
11 |
public String helloStudent(String name) {
|
13 |
return "hello " + name;
|
server类: com.jettyServer.ServerForJetty:
01 |
package com.jettyServer;
|
03 |
import javax.xml.ws.Endpoint;
|
05 |
import com.service.impl.StudentServiceImpl;
|
07 |
public class ServerForJetty {
|
09 |
public static void main(String[] args) throws InterruptedException {
|
11 |
StudentServiceImpl implementor = new StudentServiceImpl();
|
13 |
String address = "http://localhost:9000/student" ;
|
15 |
Endpoint.publish(address,implementor);
|
client类:com.client.Test
03 |
import org.apache.cxf.endpoint.Client;
|
05 |
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicclientFactory;
|
09 |
public static void main(String[] args) throws Exception {
|
11 |
JaxWsDynamicclientFactory dcf = JaxWsDynamicclientFactory.newInstance();
|
1 |
Client client = dcf.createClient( "http://localhost:9000/student?wsdl" );
|
3 |
Object[] res = client.invoke( "helloStudent" , "LeeThinker" );
|
5 |
System.out.println( "Echo response: " + res[ 0 ]);
|
先启动JettyServer, 后访问http://localhost:9000/student?wsdl, 成功刷新出wsdl, Ok, 服务顺利启动!
再执行Test的main方法, Congratulations! 这次真正使用Dynamic的方式, 在不需要通过wsdl2java生成客户端java类文件的情况下, 成功调用WebService服务!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。