cxf例子

1.新建一个web工程,并导入相关的lib

 

2.新建两个类HelloService和HelloServiceImpl

package com.pwp.webservice.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

/**
 *
 * @author Administrator
 */
@WebService
public interface HelloService {
    @WebMethod
    String sayHi(String name);

}
 
package com.pwp.webservice.service.impl;

import com.pwp.webservice.service.HelloService;
import javax.jws.WebService;

/**
 *
 * @author Administrator
 */
@WebService(endpointInterface = "com.pwp.webservice.service.HelloService")
public class HelloServiceImpl implements HelloService{

    public String sayHi(String name) {
        return name+" hello~!";
    }

}

3.在web-inf目录新赠一个bean.xml内容如下

<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jaxws="http://cxf.apache.org/jaxws"
      xmlns:jaxrs="http://cxf.apache.org/jaxrs"
      xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd">

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint
	  id="helloWorld"
	  implementor="com.pwp.webservice.service.impl.HelloServiceImpl"
	  address="/HelloWorld" />

</beans>

 4.配置web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>cxfservlet</servlet-name>
        <servlet-class>
            org.apache.cxf.transport.servlet.CXFServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxfservlet</servlet-name>
        <url-pattern>/ws/*</url-pattern>
    </servlet-mapping>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于Apache CXFweb service的例子: 1. 首先,创建一个Maven项目,并添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.7</version> </dependency> </dependencies> ``` 2. 创建一个接口,并定义web service的方法: ```java @WebService public interface GreetingService { @WebMethod String sayHello(String name); } ``` 3. 创建一个实现类,并实现接口中定义的方法: ```java @Service @WebService(endpointInterface = "com.example.demo.GreetingService") public class GreetingServiceImpl implements GreetingService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 4. 在配置文件中配置CXF: ```yaml cxf: servlet: load-on-startup: 1 url-pattern: /services/* jaxws: properties: javax.xml.ws.wsdl.service: GreetingService javax.xml.ws.wsdl.port: GreetingServicePort ``` 5. 部署应用程序并启动服务器,访问以下URL即可访问web service: ``` http://localhost:8080/services/GreetingService?wsdl ``` 6. 测试web service: ```java public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(GreetingService.class); factory.setAddress("http://localhost:8080/services/GreetingService"); GreetingService client = (GreetingService) factory.create(); String result = client.sayHello("World"); System.out.println(result); } } ``` 以上就是一个简单的基于Apache CXFweb service例子

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值