首页 > 代码库 > WebService -- Java 实现之 CXF (WebService 服务器端接口)
WebService -- Java 实现之 CXF (WebService 服务器端接口)
1. 使用Maven创建一个quickstart项目
2. 引入依赖的Jar包
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.5</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5</version></dependency><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.5</version></dependency>
3. 编写Service 接口 + 实现类
package com.example.tuo.webservice;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String sb);
}
package com.example.tuo.webservice.impl;import javax.jws.WebService;import com.example.tuo.webservice.HelloWorld;@WebServicepublic class HelloWorldImpl implements HelloWorld{ public String sayHello(String sb) { // TODO Auto-generated method stub return "Hello world," +sb; }}
4. 启动服务
package com.example.tuo.server;import org.apache.cxf.jaxws.JaxWsServerFactoryBean;import com.example.tuo.webservice.HelloWorld;import com.example.tuo.webservice.impl.HelloWorldImpl;public class Server { public static void main(String[] args){ System.out.println("web service starting..."); JaxWsServerFactoryBean wsSvrFactoryBean = new JaxWsServerFactoryBean(); String address = "http://127.0.0.1/helloWorld"; wsSvrFactoryBean.setAddress(address); wsSvrFactoryBean.setServiceClass(HelloWorld.class); HelloWorld implementor = new HelloWorldImpl(); wsSvrFactoryBean.setServiceBean(implementor); wsSvrFactoryBean.create(); System.out.println("web service started..."); }}
5. 访问服务
PS:由于我们还没有绑定成员,server会抛出异常:No binding operation info while invoking unknown method with params unknown.
至此,我们的第一个基于CXF的webservice已经编写完成并发布出来了。
WebService -- Java 实现之 CXF (WebService 服务器端接口)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。