服务接口及实现类很简单:
接口 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服务!