javaEE CXF,WebService框架

Apache-CXF框架的Jar 包下载:https://pan.baidu.com/s/14qw-C_wMrHdOzrp2diN4Cw   密码:44x4

服务端:

web.xml(网站核心配置文件,配置服务的Servlet,指定cxf.xml文件位置):

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>cxf_service</display-name>
  

  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:cxf.xml</param-value>  <!-- 通过上下文参数指定Spring配置文件的位置。默认是WEB-INF/applicationContext.xml -->
  </context-param>
  
  <!-- 创建Spring容器的监听器,让项目启动时创建Spring容器,注册服务 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置CXF框架提供的Servlet -->
  <servlet>
  	<servlet-name>cxf</servlet-name>
  	<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  	<!-- 通过初始化参数指定CXF框架的配置文件位置 -->  
  	<!-- <init-param>
  		<param-name>config-location</param-name>
  		<param-value>classpath:cxf.xml</param-value> <!-- 默认的配置文件位置是 WEB-INF/cxf-servlet.xml
  	</init-param> -->  <!-- 通过Spring框架中创建Spring容器的监听器,让项目启动时就注册服务,而不是访问Servlet时,注册服务 -->
  </servlet>
  <servlet-mapping>
  	<servlet-name>cxf</servlet-name>
  	<url-pattern>/service/*</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

cxf.xml(CXF核心配置文件,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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://cxf.apache.org/bindings/soap 
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 前面的是Spring的约束文件,后5个是CXF的约束文件 -->

	<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
	<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-servlet.xml" />
	
	<bean id="helloService" class="com.xxx.service.HelloServiceImpl"/>
	
	<!-- 注册服务(发布服务) -->    <!-- http://ip:端口号/项目名/Servlet的url/cxfService?wsdl   ip是部署服务的ip,端口号是Tomcat端口号 -->
	<jaxws:server id="myService" address="/cxfService">
		<jaxws:serviceBean>
			<ref bean="helloService"/>
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

HelloService.java(接口,添加@WebService注解):

package com.xxx.service;

import javax.jws.WebService;

@WebService
public interface HelloService {
	public String sayHello(String name);
}

HelloServiceImpl.java(接口实现类,服务的具体逻辑):

package com.xxx.service;

public class HelloServiceImpl implements HelloService{
	public String sayHello(String name) {
		System.out.println("基于CXF开发的服务端sayHello方法被调用了。。。。");
		return "hello " + name;
	}
}

客户端:

方式一(JDK提供的wsimport命令生成本地Java代码)(不推荐):

cmd---wsimport -s c:/aa http://192.168.0.1/hello?wsdl    (可以通过-p com.xxx.client来指定生成的java代码的包)

App.java(客户端,调用服务):

package com.xxx.client;

public class App {
	public static void main(String[] args) {
		HelloServiceImplService ss = new HelloServiceImplService();
		//创建客户端代理对象,用于远程调用
		HelloService proxy = ss.getHelloServiceImplPort();
		String result = proxy.sayHello("小明");  //调用远程方法
		System.out.println(result);
	}

}

方式二(基于CXF框架的客户端)(推荐):

也需要导入CXF的Jar包。

使用wsimport命令或者CXF提供wsdl2java生成本地代码;只需要生成的接口类

cmd---cd c:/apache-cxf-2.4.2/bin ---wsdl2java -d c:/aa  -p com.xxx.client http://ip:端口/项目名/Servlet的url/cxf配置中的Address?wsdl   (只需要接口HelloService.java)

HelloService.java(生成的本地代码,只需要该接口):

package com.xxx.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

@WebService(targetNamespace = "http://service.itheima.com/", name = "HelloService")
@XmlSeeAlso({})  //去掉{}中生成的内容
public interface HelloService {

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "sayHello", targetNamespace = "http://service.itheima.com/", className = "cn.itcast.client.SayHello")
    @WebMethod
    @ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://service.itheima.com/", className = "cn.itcast.client.SayHelloResponse")
    public java.lang.String sayHello(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );
}

App.java(客户端,调用服务):

package com.xxx.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	public static void main(String[] args) {
		//创建Spring工厂
		ApplicationContext ctx = new ClassPathXmlApplicationContext("cxf.xml");
		// 创建代理对象
		HelloService proxy = (HelloService) ctx.getBean("myClient");
		String result = proxy.sayHello("test");   //调用远程方法
		System.out.println(result);
	}
}

cxf.xml(CXF核心配置文件,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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
					http://www.springframework.org/schema/beans/spring-beans.xsd
					http://cxf.apache.org/bindings/soap 
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 前面的是Spring的约束文件,后5个是CXF的约束文件 -->

	<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
	<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-servlet.xml" />
	
	<!-- 注册CXF客户端代理对象,通过spring框架创建这个代理对象,通过代理对象实现远程调用 -->
	<jaxws:client id="myClient" 
				address="http://192.168.0.1:8080/cxf_service/service/cxfService" 
				serviceClass="com.xxx.client.HelloService">
	</jaxws:client>
</beans>

扫描二维码关注公众号,回复: 3286521 查看本文章

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82683021
今日推荐