今天和你们分享下 使用maven 搭建 webService 服务端:java
首先须要在你的IDE中集成Maven。集成办法此处略。。。。。。。web
1.建立一个web工程。spring
2.在pom文件中增长如下依赖:apache
<!-- spring core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>2.5.5</version> </dependency> <!-- spring beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>2.5.5</version> </dependency> <!-- spring context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>2.5.5</version> </dependency> <!-- spring web --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>2.5.5</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxb-api</artifactId> <version>2.1</version> <type>pom</type> </dependency> <dependency> <groupId>javax.xml</groupId> <artifactId>jaxb-impl</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>xfire</groupId> <artifactId>saaj-api</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>xfire</groupId> <artifactId>saaj-impl</artifactId> <version>1.3</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.2</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.2.3</version> </dependency>
3.建立服务接口:api
包路径 net.cc.helloworldapp
其中:frontend
1. @webService 表示这是一个webServicemaven
2. @webParam 说明这个参数的名称ide
package net.cc.helloworld; import javax.jws.WebParam; import javax.jws.WebService; /** * @author test * @create 2013-11-21下午09:48:01 */ @WebService public interface HelloWorld { String sayHello(@WebParam(name = "text") String text); }
4.建立实现接口:ui
包路径 net.cc.helloworld
其中:
1. @webService(serviceName = “HelloWorld”) 应予实现的接口名称保持一致
package net.cc.helloworld; import javax.jws.WebService; /** * @author test * @create 2013-11-21下午09:50:31 */ @WebService(serviceName = "HelloWorld") public class HelloWorldImpl implements HelloWorld { @Override public String sayHello(String text) { // TODO Auto-generated method stub System.out.println(text); return "say " + text; } }
5. 发布 服务
能够使用如下方式发布服务:(此发布方式不是惟一的,后续章节会提供web版本发布方式)
package net.cc.server; import javax.xml.ws.Endpoint; import net.cc.helloworld.HelloWorld; import net.cc.helloworld.HelloWorldImpl; /** * @author test * @create 2013-11-21下午09:55:49 */ public class Servers { public Servers() { HelloWorld hello = new HelloWorldImpl(); String address = "http://192.168.1.107:9000/HelloWorld"; Endpoint.publish(address, hello); } public static void main(String[] args) { new Servers(); System.out.println("server start ..."); } }
6. 启动成功后 会在控制台看到相似的语句,表示发布成功。
log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext). log4j:WARN Please initialize the log4j system properly. 十一月 21, 2013 11:07:25 下午 org.apache.cxf.bus.spring.BusApplicationContext getConfigResources INFO: No cxf.xml configuration file detected, relying on defaults. 十一月 21, 2013 11:07:26 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass INFO: Creating Service {http://helloworld.cc.net/}HelloWorld from class net.cc.helloworld.HelloWorld 十一月 21, 2013 11:07:26 下午 org.apache.cxf.endpoint.ServerImpl initDestination INFO: Setting the server's publish address to be http://192.168.1.107:9000/HelloWorld 十一月 21, 2013 11:07:26 下午 org.mortbay.log.Slf4jLog info INFO: Logging to org.slf4j.impl.JDK14LoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog 十一月 21, 2013 11:07:26 下午 org.mortbay.log.Slf4jLog info INFO: jetty-6.1.19 十一月 21, 2013 11:07:26 下午 org.mortbay.log.Slf4jLog info INFO: Started SelectChannelConnector@0.0.0.0:9000 server start ...
此时 打开游览器 输入 http://IP:9000/HelloWorld?wsdl 便可看到发布的webService