今天和你們分享下 使用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