今天和大家分享下 使用maven 搭建 webService 服务端:
首先需要在你的IDE中集成Maven。集成办法此处略。。。。。。。
1.创建一个web工程。
2.在pom文件中增加以下依赖:
1 <!-- spring core --> 2 <dependency> 3 <groupId>org.springframework</groupId> 4 <artifactId>spring-core</artifactId> 5 <version>2.5.5</version> 6 </dependency> 7 8 <!-- spring beans --> 9 <dependency> 10 <groupId>org.springframework</groupId> 11 <artifactId>spring-beans</artifactId> 12 <version>2.5.5</version> 13 </dependency> 14 15 <!-- spring context --> 16 <dependency> 17 <groupId>org.springframework</groupId> 18 <artifactId>spring-context</artifactId> 19 <version>2.5.5</version> 20 </dependency> 21 22 <!-- spring web --> 23 <dependency> 24 <groupId>org.springframework</groupId> 25 <artifactId>spring-web</artifactId> 26 <version>2.5.5</version> 27 </dependency> 28 29 <dependency> 30 <groupId>commons-logging</groupId> 31 <artifactId>commons-logging</artifactId> 32 <version>1.1</version> 33 </dependency> 34 35 <dependency> 36 <groupId>javax.xml</groupId> 37 <artifactId>jaxb-api</artifactId> 38 <version>2.1</version> 39 <type>pom</type> 40 </dependency> 41 42 <dependency> 43 <groupId>javax.xml</groupId> 44 <artifactId>jaxb-impl</artifactId> 45 <version>2.1</version> 46 </dependency> 47 48 <dependency> 49 <groupId>xfire</groupId> 50 <artifactId>saaj-api</artifactId> 51 <version>1.3</version> 52 </dependency> 53 54 <dependency> 55 <groupId>xfire</groupId> 56 <artifactId>saaj-impl</artifactId> 57 <version>1.3</version> 58 </dependency> 59 60 <dependency> 61 <groupId>wsdl4j</groupId> 62 <artifactId>wsdl4j</artifactId> 63 <version>1.6.2</version> 64 </dependency> 65 66 <dependency> 67 <groupId>org.apache.cxf</groupId> 68 <artifactId>cxf-rt-frontend-jaxws</artifactId> 69 <version>2.2.3</version> 70 </dependency> 71 <dependency> 72 <groupId>org.apache.cxf</groupId> 73 <artifactId>cxf-rt-transports-http</artifactId> 74 <version>2.2.3</version> 75 </dependency> 76 <dependency> 77 <groupId>org.apache.cxf</groupId> 78 <artifactId>cxf-rt-transports-http-jetty</artifactId> 79 <version>2.2.3</version> 80 </dependency>
3.创建服务接口:
包路径 net.cc.helloworld
其中:
1. @webService 表示这是一个webService
2. @webParam 说明这个参数的名称
1 package net.cc.helloworld; 2 3 import javax.jws.WebParam; 4 import javax.jws.WebService; 5 6 /** 7 * @author test 8 * @create 2013-11-21下午09:48:01 9 */ 10 @WebService 11 public interface HelloWorld { 12 13 String sayHello(@WebParam(name = "text") String text); 14 15 }
4.创建实现接口:
包路径 net.cc.helloworld
其中:
1. @webService(serviceName = “HelloWorld”) 应予实现的接口名称保持一致
1 package net.cc.helloworld; 2 3 import javax.jws.WebService; 4 5 /** 6 * @author test 7 * @create 2013-11-21下午09:50:31 8 */ 9 @WebService(serviceName = "HelloWorld") 10 public class HelloWorldImpl implements HelloWorld { 11 12 @Override 13 public String sayHello(String text) { 14 // TODO Auto-generated method stub 15 System.out.println(text); 16 return "say " + text; 17 } 18 19 }
5. 发布 服务
可以使用以下方式发布服务:(此发布方式不是唯一的,后续章节会提供web版本发布方式)
1 package net.cc.server; 2 3 import javax.xml.ws.Endpoint; 4 5 import net.cc.helloworld.HelloWorld; 6 import net.cc.helloworld.HelloWorldImpl; 7 8 /** 9 * @author test 10 * @create 2013-11-21下午09:55:49 11 */ 12 public class Servers { 13 14 public Servers() { 15 16 HelloWorld hello = new HelloWorldImpl(); 17 String address = "http://192.168.1.107:9000/HelloWorld"; 18 Endpoint.publish(address, hello); 19 } 20 21 public static void main(String[] args) { 22 23 new Servers(); 24 System.out.println("server start ..."); 25 } 26 }
6. 启动成功后 会在控制台看到类似的语句,表示发布成功。
1 log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext). 2 log4j:WARN Please initialize the log4j system properly. 3 十一月 21, 2013 11:07:25 下午 org.apache.cxf.bus.spring.BusApplicationContext getConfigResources 4 INFO: No cxf.xml configuration file detected, relying on defaults. 5 十一月 21, 2013 11:07:26 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 6 INFO: Creating Service {http://helloworld.cc.net/}HelloWorld from class net.cc.helloworld.HelloWorld 7 十一月 21, 2013 11:07:26 下午 org.apache.cxf.endpoint.ServerImpl initDestination 8 INFO: Setting the server's publish address to be http://192.168.1.107:9000/HelloWorld 9 十一月 21, 2013 11:07:26 下午 org.mortbay.log.Slf4jLog info 10 INFO: Logging to org.slf4j.impl.JDK14LoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog 11 十一月 21, 2013 11:07:26 下午 org.mortbay.log.Slf4jLog info 12 INFO: jetty-6.1.19 13 十一月 21, 2013 11:07:26 下午 org.mortbay.log.Slf4jLog info 14 INFO: Started SelectChannelConnector@0.0.0.0:9000 15 server start ...
此时 打开游览器 输入 http://192.168.1.107:9000/HelloWorld?wsdl 即可看到发布的webService