apache cxf笔记之一个简单的JAX-WS服务程序

来源:互联网 发布:gta5第一人称视角优化 编辑:程序博客网 时间:2024/04/30 07:44

推荐一本apache cxf的书籍:

apache cxf的配置,这边就不做介绍了。请参照我关于它配置的博文。

开发步骤:

1、新建Java project,build path引入cxf runtime。

2、新建SEI服务接口:

package demo.cxf.helloworld;import javax.jws.WebParam;public interface HelloWorld {public String sayHello(@WebParam(name="text")String text);//注解定义web参数的}

3、服务的实现:

package demo.cxf.helloworld;import javax.jws.WebParam;import javax.jws.WebService;@WebService(name="HelloWorld")//注解定义服务public class HelloWorldImpl implements HelloWorld {@Overridepublic String sayHello(@WebParam(name="text")String text) {System.out.println("sayHello called");return "Hello "+text+",this is world!";}}

4、服务的发布与测试

package demo.cxf.helloworld.server;import java.io.IOException;import javax.xml.ws.Endpoint;import demo.cxf.helloworld.HelloWorld;import demo.cxf.helloworld.HelloWorldImpl;public class Server {public static void main(String[] args) throws IOException {Server server = new Server();server.startServer();System.out.println("Startting ready...");System.in.read();//按任意键退出System.out.println("Server exiting...");System.exit(0);//程序退出}public void startServer(){System.out.println("Starting Server.....");HelloWorld helloWorld = new HelloWorldImpl();String address = "http://localhost:8080/HelloWorld";//发布服务Endpoint.publish(address, helloWorld);}}

测试:run as java application

浏览器访问:http://localhost:8080/HelloWorld?wsdl


原创粉丝点击