cxf和spring开发web service--服务器端
2016-11-02 13:43
417 查看
服务器端
1. 编写服务
首先编写服务接口,例子很简单,任何调用这个接口,会返回“Hello" + 输入内容
package demo.spring.service;
import javax.jws.WebService;
/**
File: HelloWorld.java
Description: TODO
@author
@date 2016年11月2日 下午1:39:46
@version 1.0
**/
@WebService
public interface HelloWorld {
String sayHi(String text);
}
编写实现类:
package demo.spring.service;
import javax.jws.WebService;
/**
File: HelloWorldImpl.java
Description: TODO
@author
@date 2016年11月2日 下午1:40:58
@version 1.0
**/
@WebService(endpointInterface="demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHi(String text) {
System.out.println("Server sayHi called");
return "Hello " + text;
}
}
@Webservice注解,告诉cxf在创建wsdl时应该使用哪个接口
2. 编写spring bean
再web-inf目录下新建beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>
jaxws endpoint的bean由3部分组成
id 在spring context中bean的id
implementor 指定负责执行的class名(犬类名)
address 指定服务提供的路径,相对路径,例如上面address=/helloworld,则真正wsdl的路径为: http://localhost:8080/projectname/helloworld?wsdl
3. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: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>/*</url-pattern>
</servlet-mapping>
</web-app>
配置完成后,部署运行
http://localhost:8080/springwebservice/
wsdl路径为: http://localhost:8080/springwebservice/HelloWorld?wsdl
客户端
1. 获取wsdl的路径 http://localhost:8080/springwebservice/HelloWorld?wsdl
进入src目录,运行指令生成客户端代码:
wsimport -keep http://localhost:8080/springwebservice/HelloWorld?wsdl
然后编写测试代码:
HelloWorldImplService factory = new HelloWorldImplService();
HelloWorld helloWorld = factory.getHelloWorldImplPort();
String value = helloWorld.sayHi("某某某");
System.out.println(value);
运行测试代码
客户端和服务器端均有相关打印信息,则测试完成
1. 编写服务
首先编写服务接口,例子很简单,任何调用这个接口,会返回“Hello" + 输入内容
package demo.spring.service;
import javax.jws.WebService;
/**
File: HelloWorld.java
Description: TODO
@author
@date 2016年11月2日 下午1:39:46
@version 1.0
**/
@WebService
public interface HelloWorld {
String sayHi(String text);
}
编写实现类:
package demo.spring.service;
import javax.jws.WebService;
/**
File: HelloWorldImpl.java
Description: TODO
@author
@date 2016年11月2日 下午1:40:58
@version 1.0
**/
@WebService(endpointInterface="demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHi(String text) {
System.out.println("Server sayHi called");
return "Hello " + text;
}
}
@Webservice注解,告诉cxf在创建wsdl时应该使用哪个接口
2. 编写spring bean
再web-inf目录下新建beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="helloWorld" implementor="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>
jaxws endpoint的bean由3部分组成
id 在spring context中bean的id
implementor 指定负责执行的class名(犬类名)
address 指定服务提供的路径,相对路径,例如上面address=/helloworld,则真正wsdl的路径为: http://localhost:8080/projectname/helloworld?wsdl
3. 配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath: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>/*</url-pattern>
</servlet-mapping>
</web-app>
配置完成后,部署运行
http://localhost:8080/springwebservice/
wsdl路径为: http://localhost:8080/springwebservice/HelloWorld?wsdl
客户端
1. 获取wsdl的路径 http://localhost:8080/springwebservice/HelloWorld?wsdl
进入src目录,运行指令生成客户端代码:
wsimport -keep http://localhost:8080/springwebservice/HelloWorld?wsdl
然后编写测试代码:
HelloWorldImplService factory = new HelloWorldImplService();
HelloWorld helloWorld = factory.getHelloWorldImplPort();
String value = helloWorld.sayHi("某某某");
System.out.println(value);
运行测试代码
客户端和服务器端均有相关打印信息,则测试完成
相关文章推荐
- Spring boot 整合CXF开发web service
- SpringBoot + CXF 开发Web Service 服务
- Spring boot 整合CXF开发web service
- 用cxf做基于spring的web service开发(八)
- 用cxf+spring开发web service
- Spring boot 整合CXF开发web service示例
- Spring boot 整合CXF开发web service
- 使用CXF开发web service 的服务器端(三)
- Web Service ---- CXF + SPRING 开发 SOAP服务
- Web Service笔记(四):利用CXF结合Spring开发web service
- maven+spring+cxf编写web service (2)
- maven+spring+cxf编写web service
- Apache CXF开发Web Service 理解CXF Frontends之Code-First
- 是用CXF开发Web Service
- 2.利用CFX开发Web Service的服务器端:实现类
- CXF + Spring 开发 Webservices
- Apache CXF+Spring开发环境搭建小试
- CXF+Spring 开发WebService
- 使用CXF和Spring搭建Web Service
- Web Service__CXF__两种开发的两种方式