今天和大家分享下 使用maven 搭建 webService 服务端:
首先需要在你的IDE中集成Maven。集成办法此处略。。。。。。。
1.创建一个web工程。
2.在pom文件中增加以下依赖:
org.springframework
spring-core
2.5.5
org.springframework
spring-beans
2.5.5
org.springframework
spring-context
2.5.5
org.springframework
spring-web
2.5.5
commons-logging
commons-logging
1.1
javax.xml
jaxb-api
2.1
pom
javax.xml
jaxb-impl
2.1
xfire
saaj-api
1.3
xfire
saaj-impl
1.3
wsdl4j
wsdl4j
1.6.2
org.apache.cxf
cxf-rt-frontend-jaxws
2.2.3
org.apache.cxf
cxf-rt-transports-http
2.2.3
org.apache.cxf
cxf-rt-transports-http-jetty
2.2.3
3.创建服务接口:
包路径 net.cc.helloworld
其中:
1. @webService 表示这是一个webService
2. @webParam 说明这个参数的名称
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.创建实现接口:
包路径 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