CXF框架提供一个和jdk提供的wsimport命令功能类似的命令wsdl2java,可以解析wsdl文件,生成本地代码

webservice (三) CXF

webservice (三) CXF

 

webservice (三) CXF

新建一个Dynamic web project

导入如下包

webservice (三) CXF

web.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>
  <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>
  
  <!-- 配置CXF框架提供的servlet -->
  <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <init-param>
       <!-- 通过初始化参数指定CXF框架的配置文件位置 -->
       <param-name>config-location</param-name>
       <param-value>classpath:cxf.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/service/*</url-pattern>
  </servlet-mapping>
</web-app>

文件结构

webservice (三) CXF

cxf.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"
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">
	<!-- 引入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.wjl.service.HelloServiceImpl"/>
	
	<!-- 注册服务 -->
	<jaxws:server id="myService" address="/cxfService">
		<jaxws:serviceBean>
			<ref bean="helloService"/>
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

HelloService.java (重点添加@WebService注解)

package com.wjl.service;

import javax.jws.WebService;
@WebService
public interface HelloService {
	public String sayHello(String name);
}

HelloServiceImpl.java

package com.wjl.service;
/**
 * wsdl文档   http://localhost:8066/cxf_service/service/cxfService?wsdl
 * @author Administrator
 *
 */
public class HelloServiceImpl implements HelloService {
	public static int num=1; 
	public String sayHello(String name) {
		System.out.println("第"+num+"调用");
		num++;
		System.out.println("基于CXF开发的服务端调用服务器sayHello方法");
		
		return "hello:"+name;
	}

}

调用方式一.

新建java项目

在命令行窗口下使用jdk自带命令

wsimport -s . http://localhost:8066/cxf_service/service/cxfService?wsdl

将会生成多个.java文件 导入新项目 ,创建AppTest.java

webservice (三) CXF

 

package com.wjl.service;

import org.junit.Test;

public class AppTest {
	@Test
	public void test1()
	{
		HelloServiceImplService ss = new HelloServiceImplService();
		HelloService proxy = ss.getHelloServiceImplPort();
		for(int i=0;i<10;i++)
		{
			String str = proxy.sayHello("Lucy");
			System.out.println(str);
		}
	}
}

 

调用方式二 使用CXF的命令 wsdl2java 

webservice (三) CXF

生成很多.java文件 只需要HelloService.java文件

webservice (三) CXF

新建一个java项目需要导包

文件目录

webservice (三) CXF

package com.wjl.client;

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

/**
 * This class was generated by Apache CXF 2.4.2
 * 2019-05-04T19:35:31.713+08:00
 * Generated source version: 2.4.2
 * 
 */
@WebService(targetNamespace = "http://service.wjl.com/", name = "HelloService")
@XmlSeeAlso({})
public interface HelloService {

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

App.java

package com.wjl.client;

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

public class App {
	@Test
	public void test()
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("cxf.xml");
		HelloService proxy = (HelloService)context.getBean("myClient");
		String info = proxy.sayHello("test");
		System.out.println(info);
	}
}

 

 

 

相关文章:

  • 2021-08-25
  • 2021-10-18
  • 2021-12-04
  • 2021-07-08
  • 2021-05-20
  • 2021-10-19
  • 2021-12-05
  • 2022-02-02
猜你喜欢
  • 2021-08-27
  • 2021-10-30
  • 2021-07-27
  • 2021-12-05
  • 2022-01-27
  • 2021-10-26
相关资源
相似解决方案