WebService 整合 Spring
2014-06-05 16:04
429 查看
WebService with
Spring
一:创建服务器端
1.1:web.xml 配置
<!--
配置Spring的ContextLoaderListener监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置CXFServlet
-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
1.2:代码编写
1.2.1//编写接口
package demo.cxf.service;
import javax.jws.WebService;
@WebService(注释一定要写)
public interface Login {
public String login(String
name,String password);
}
2.2.2//实现接口方法
package demo.cxf.service;
import javax.jws.WebService;
@WebService(endpointInterface="demo.cxf.service.Login",serviceName="Login")(注释一定要写)
public class LoginImpl implements
Login {
@Override
public String login(String name,
String password) {
String result =
"登陆CXF服务端成功!";
if(!"cxf".equalsIgnoreCase(name)||
!"cxf".equalsIgnoreCase(password)){
result = "用户名或密码不正确,请重新登陆!";
}
return result;
}
}
1.3:编写Spring配置文件
<?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">
<!-- 引入cxf的两个配置文件 -->
<import
resource="classpath:META-INF/cxf/cxf.xml"/>
<import
resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- 通过<jaxw:endpoint创建一个服务终端
-->
<!-- implementor:服务端实现类 -->
<!-- id:spring上下文中的bean的id -->
<!--address:服务请求地址 -->
<!--
<jaxws:endpoint
id="helloWorld"
implementor="demo.spring.service.HelloWorldImpl"
address="/HelloWorld">
</jaxws:endpoint>
-->
<jaxws:endpoint id="login"
implementor="demo.cxf.service.LoginImpl"
address="/Login">
</jaxws:endpoint>
</beans>
1.4:启动服务器
http://localhost:8088/mycxf/Login?wsdl
如果看到配置文件---------就表示服务端配置成功.
在Eclipse里打包就可以用啦 Export jar 本文中Spring
配置文件为 cxf-servlet.xml
二:创建客户端(有两种方法:一种是不要Spring管理。一种需要Spring管理)
2.1:导入相应的包,包括上面打的包
2.2:不要Spring管理
代码
package
demo.cxf.client;
import
org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import
demo.cxf.service.Login;
public class CXFClient {
public static void main(String[]
args) {
JaxWsProxyFactoryBean jwpFactory =
new JaxWsProxyFactoryBean();
jwpFactory.setAddress("http://localhost:8088/mycxf/Login?wsdl");
jwpFactory.setServiceClass(Login.class);
Login login =
(Login)jwpFactory.create();
String result = login.login("cxf",
"cxf");
System.out.println("登陆结果:"+result);
}
}
2.3:需要Spring管理的(web.xml不要在配置CXF启动啦)
2.3.1
配置Spring配置文件(客户端配置有2种方法)
<?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">
<!-- 配置客户端代码方法一 -->
<jaxws:client
id="helloClient"
serviceClass="demo.cxf.service.Login"只能填写Login接口代理类
address="http://localhost:8088/mycxf/Login"
></jaxws:client>
<!-- 配置客户端代码方法二 -->
<bean id="client"
class="demo.cxf.service.Login"//填Login接口和LoginImpl都可以
factory-bean="clientFactory"
factory-method="create">
</bean>
<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="demo.cxf.service.Login"></property>只能填写Login接口代理类
<property name="address"
value="http://localhost:8088/mycxf/Login"></property>
</bean>
</beans>
2.3.2 编写代码
package
demo.spring.client;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
demo.cxf.service.Login;
import
demo.spring.service.HelloWorld;
public class Client {
public static void main(String[]
args) {
ClassPathXmlApplicationContext
context
= new
ClassPathXmlApplicationContext("cxf-servlet.xml");
Login client =
(Login)context.getBean("helloClient");
String response =
client.login("cxf", "cxf");
System.out.println("Response:"+response);
System.exit(0);
}
}
Spring
一:创建服务器端
1.1:web.xml 配置
<!--
配置Spring的ContextLoaderListener监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置CXFServlet
-->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
1.2:代码编写
1.2.1//编写接口
package demo.cxf.service;
import javax.jws.WebService;
@WebService(注释一定要写)
public interface Login {
public String login(String
name,String password);
}
2.2.2//实现接口方法
package demo.cxf.service;
import javax.jws.WebService;
@WebService(endpointInterface="demo.cxf.service.Login",serviceName="Login")(注释一定要写)
public class LoginImpl implements
Login {
@Override
public String login(String name,
String password) {
String result =
"登陆CXF服务端成功!";
if(!"cxf".equalsIgnoreCase(name)||
!"cxf".equalsIgnoreCase(password)){
result = "用户名或密码不正确,请重新登陆!";
}
return result;
}
}
1.3:编写Spring配置文件
<?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">
<!-- 引入cxf的两个配置文件 -->
<import
resource="classpath:META-INF/cxf/cxf.xml"/>
<import
resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!-- 通过<jaxw:endpoint创建一个服务终端
-->
<!-- implementor:服务端实现类 -->
<!-- id:spring上下文中的bean的id -->
<!--address:服务请求地址 -->
<!--
<jaxws:endpoint
id="helloWorld"
implementor="demo.spring.service.HelloWorldImpl"
address="/HelloWorld">
</jaxws:endpoint>
-->
<jaxws:endpoint id="login"
implementor="demo.cxf.service.LoginImpl"
address="/Login">
</jaxws:endpoint>
</beans>
1.4:启动服务器
http://localhost:8088/mycxf/Login?wsdl
如果看到配置文件---------就表示服务端配置成功.
在Eclipse里打包就可以用啦 Export jar 本文中Spring
配置文件为 cxf-servlet.xml
二:创建客户端(有两种方法:一种是不要Spring管理。一种需要Spring管理)
2.1:导入相应的包,包括上面打的包
2.2:不要Spring管理
代码
package
demo.cxf.client;
import
org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import
demo.cxf.service.Login;
public class CXFClient {
public static void main(String[]
args) {
JaxWsProxyFactoryBean jwpFactory =
new JaxWsProxyFactoryBean();
jwpFactory.setAddress("http://localhost:8088/mycxf/Login?wsdl");
jwpFactory.setServiceClass(Login.class);
Login login =
(Login)jwpFactory.create();
String result = login.login("cxf",
"cxf");
System.out.println("登陆结果:"+result);
}
}
2.3:需要Spring管理的(web.xml不要在配置CXF启动啦)
2.3.1
配置Spring配置文件(客户端配置有2种方法)
<?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">
<!-- 配置客户端代码方法一 -->
<jaxws:client
id="helloClient"
serviceClass="demo.cxf.service.Login"只能填写Login接口代理类
address="http://localhost:8088/mycxf/Login"
></jaxws:client>
<!-- 配置客户端代码方法二 -->
<bean id="client"
class="demo.cxf.service.Login"//填Login接口和LoginImpl都可以
factory-bean="clientFactory"
factory-method="create">
</bean>
<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="demo.cxf.service.Login"></property>只能填写Login接口代理类
<property name="address"
value="http://localhost:8088/mycxf/Login"></property>
</bean>
</beans>
2.3.2 编写代码
package
demo.spring.client;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
demo.cxf.service.Login;
import
demo.spring.service.HelloWorld;
public class Client {
public static void main(String[]
args) {
ClassPathXmlApplicationContext
context
= new
ClassPathXmlApplicationContext("cxf-servlet.xml");
Login client =
(Login)context.getBean("helloClient");
String response =
client.login("cxf", "cxf");
System.out.println("Response:"+response);
System.exit(0);
}
}
相关文章推荐
- webservice 入门
- Web Service简介
- 【Spring】Spring&WEB整合原理及源码剖析
- SSH 整合- 4 - add service_servic…
- SSH整合-&nbsp;2-&nbsp;add&nbsp;service&nbsp;layout
- gSoap开发webservice&nbsp;client
- WebService Xfire+spring整合开发,哪位大哥给些资料。
- CXF WebService整合Spring
- CXF WebService整合Spring
- SSH整合- 2- add service layout
- CXF WebService整合Spring
- CXF WebService整合Spring
- 如何用C++调用远程Web&nbsp;Service
- 【Spring】Spring&WEB整合原理及源码分析
- CXF WebService整合Spring
- 白痴理解的SOAP/Web Service/…
- WebService 创建 发布 调用整个流…
- Spring整合开发---Struts 2(注解…
- SSH 整合- 6 - service_serviceImp…
- CXF WebService整合Spring