`

java使用cxf发布webservice

阅读更多
1.去官网下载jar包 http://cxf.apache.org/download.html
2.配置web.xml文件,见红色字体部分
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			/WEB-INF/classes/applicationContext*.xml
		</param-value>
	</context-param>
	
	<!-- 拦截器 -->
	 <filter>
		<filter-name>RequestFilter</filter-name>
		<filter-class>com.creditwe.framework.common.utils.RequestLoginFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>RequestFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>RequestFilter</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>
	
	<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
	
	
	<filter>
		<filter-name>requestContextFilter</filter-name>
		<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>requestContextFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- spring监听器 -->

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
   	
	

	<!-- 设置session失效时间 -->
	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>
	
	<welcome-file-list>
		<welcome-file>login.jsp</welcome-file>
	</welcome-file-list>

	<!-- cxf配置 -->
	<servlet>
        <servlet-name>CXFService</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>
    <servlet-mapping>  
        <servlet-name>CXFService</servlet-name>  
        <url-pattern>/services/*</url-pattern>  
    </servlet-mapping>
	
</web-app>

注意<url-pattern>/services/*</url-pattern>配置, CXFServlet 会拦截此类 url ,进行处理 。上面配置的webservice 将通过如下 URL 访问到:
http://localhost:8080/creditmatchservice/services/bu_lendmoney
UserManager 为 <jaxws:endpoint> 标签中address 属性对应的值
creditmatchservice 为本项目的名称
配置完成之后,将项目部署到tomcat 上
输入URL : http://localhost:8080/creditmatchservice/services/bu_lendmoney?wsdl
将会看到生成的wsdl 文件
2. 配置applicationContext-cxf-spring.xml文件,需要引入CXF的XML Scheam 配置文件和cxf的资源文件,这三个文件在 cxf.jar 中:<?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:context="http://www.springframework.org/schema/context"
	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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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-servlet.xml" />


	
<bean id="lendmoney" class="com.creditwe.businessinterface.lendmoneyservice.impl.lendmoneyServiceImpl" />
	<!-- 业务系统传入借款数据 -->
        <jaxws:endpoint id="bu_lendmoneyService" implementor="#lendmoney" address="/bu_lendmoney" />
</beans>
注意:
①、 address   为webservice 发布的地址
②、 implementorClass   为该webservice 实现的接口
<!-- 另一种写法 是
 <jaxws:endpoint id="bu_lendmoneyService" implementor="com.creditwe.businessinterface.lendmoneyservice.impl.lendmoneyServiceImpl" address="/bu_lendmoney" />
 建议不要用这种方法 ,如果实现类有的属性要通过spring依赖注入的话,这种方法只是简单的new个实现类,他的属性没有通过spring依赖注入给注入值
 所有综合考虑 建议使用上面的写法!
 -->
 


 3.

webservice服务接口,使用Annotation进行标注

 

 

 

先了解各种类型的Annotation

@WebService注解的endpointInterface指定实现类的接口,serviceName对应于applicationContext-cxf-spring.xml文件中配置的address属性

 

package com.creditwe.businessinterface.lendmoneyservice;
import javax.jws.WebService;
/**
 * 业务系统传入借款数据
 *
 */
@WebService
public interface lendmoneyService {
	public String lendmoney(String inStr) throws Exception;
}


package com.creditwe.businessinterface.lendmoneyservice.impl;
@WebService(endpointInterface="com.creditwe.businessinterface.lendmoneyservice.lendmoneyService", serviceName="/bu_lendmoney")
public class lendmoneyServiceImpl extends BaseIfc implements lendmoneyService{
        @Override
	public String lendmoney(String inStr) throws Exception{

   }
}

 4.webservice客户端访问服务

<!-- 方式一:实现客户端 对象创建 -->
<bean id="client" class="com.lekihuo.demo.HelloWorld"
factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory"
class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass"
value="com.lekihuo.demo.HelloWorld" />
<property name="address"
value="http://localhost:8088/testcxf/webservices/HelloWorld" />
</bean>

 

 

public class WebServiceClient {
public static void main(String[] args) {
System.out.println("Hello World Client ");
JaxWsProxyFactoryBean jwpf = new JaxWsProxyFactoryBean();//代理工厂Bean
jwpf.setServiceClass(HelloWorld.class);
jwpf.setAddress("http://localhost:8082/HelloWorld");//这里要和正确访问的wsdl地址一致
//http://localhost:8088/testcxf/webservices/HelloWorld
HelloWorld hw = (HelloWorld)jwpf.create();
User user = new User();
user.setName("Tony");
user.setAge(25); 
System.out.println(hw.sayHiToUser(user));  

 

 

 

 方法二:

//=============两种方式都一样的============
// ApplicationContext context = new ClassPathXmlApplicationContext(
// new String[]{ "applicationContext.xml" });
//
// HelloWorld client = (HelloWorld) context.getBean("client");//注意检查address是否跟wsdl一致
// User user1 = new User();
// user1.setName("Tony");
// User user2 = new User();
// user2.setName("freeman");
// List<User> userList = new ArrayList<User>();
// userList.add(user1);
// userList.add(user2);
// String[] res = client.sayHiToUserList(userList);
// System.out.println(res[0]);
// System.out.println(res[1]);

 方法三:单元测试

public class Junit_CXF_Test {
        @Test
	public void testQueryObligatory() throws Exception {
		Map<String, String> req = new HashMap<String, String>();
		req.put("code", BaseIfc.getEnCode("QXB"));
		String in = JSONObject.fromObject(req).toString();
		System.out.println(in);
		// 创建WebService客户端代理工厂
		JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
		// 注册WebService接口
		factory.setServiceClass(QueryObligatoryService.class);
		// 设置WebService地址
		factory.setAddress("http://192.168.1.215:8384/creditmatchservice/services/queryObligatory");
		QueryObligatoryService f = (QueryObligatoryService) factory.create();
		String res = null;
		res = f.query(in);
		System.err.println("返回结果:" + res);
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics