1. 程式人生 > >webservice--CXF+Spring整合釋出SOAP協議的服務

webservice--CXF+Spring整合釋出SOAP協議的服務

CXF+Spring整合釋出SOAP協議的服務

服務端

開發步驟:

第一步:建立web專案(引入jar包)

第二步:建立SEI介面

package cn.itcast.ws.cxf.server;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

/**
 * 
 * <p>Title: WeatherInterface.java</p>
 * <p>Description:SEI介面</p>
 */
@WebService
@BindingType(SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {

	public String queryWeather(String cityName);
	
}

第三步:建立SEI實現類

package cn.itcast.ws.cxf.server;

/**
 * 
 * <p>Title: WeatherInterfaceImpl.java</p>
 * <p>Description:SEI實現類</p>
 */
public class WeatherInterfaceImpl implements WeatherInterface {

	@Override
	public String queryWeather(String cityName) {
		System.out.println("from client..."+cityName);
		if("北京".equals(cityName)){
			return "冷且霾";
		} else {
			return "暖且晴";
		}
	}

}

第四步:配置spring配置檔案,applicationContext.xml,

<jaxws:server標籤釋出服務,設定1.服務地址;2.設定服務介面;3設定服務實現類

<?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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
				            http://www.springframework.org/schema/beans/spring-beans.xsd
				            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
				            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
				            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
	<!-- <jaxws:server釋出SOAP協議的服務 ,對JaxWsServerFactoryBean類封裝-->
	<jaxws:server address="/weather" serviceClass="cn.itcast.ws.cxf.server.WeatherInterface">
		<jaxws:serviceBean>
			<ref bean="weatherInterface"/>
		</jaxws:serviceBean>
	</jaxws:server>
	<!-- 配置服務實現類 -->
	<bean name="weatherInterface" class="cn.itcast.ws.cxf.server.WeatherInterfaceImpl"/>
</beans>

第五步:配置web.xml,配置spring配置檔案地址和載入的listener,配置CXFservlet

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ws_2_cxf_spring_server</display-name>
  
  <!-- 設定spring的環境 -->
  <context-param>
  	<!--contextConfigLocation是不能修改的  -->
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <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>
  </servlet>
  <servlet-mapping>
  	<servlet-name>CXF</servlet-name>
  	<url-pattern>/ws/*</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>

第六步:部署到tomcat下,啟動tomcat

第七步:測試服務,閱讀使用說明書


攔截器配置

配置applicationContext.xml中。


Endpoint標籤釋出服務

<jaxws:endpoint>標籤

import javax.jws.WebService;

/**
 * 
 * <p>Title: HelloWorld.java</p>
 * <p>Description:簡單類</p>
 */
@WebService
public class HelloWorld {
	public String sayHello(String name){
		return "hello,"+name;
	}
	
}

applicationContext.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
				            http://www.springframework.org/schema/beans/spring-beans.xsd
				            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
				            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
				            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
	<!-- <jaxws:endpoint釋出SOAP協議的服務 ,對Endpoint類封裝-->	
	<jaxws:endpoint address="/hello" implementor="cn.itcast.ws.cxf.server.HelloWorld"/>		            
	
	<!-- <jaxws:server釋出SOAP協議的服務 ,對JaxWsServerFactoryBean類封裝-->
	<jaxws:server address="/weather" serviceClass="cn.itcast.ws.cxf.server.WeatherInterface">
		<jaxws:serviceBean>
			<ref bean="weatherInterface"/>
		</jaxws:serviceBean>
		
		<!-- 配置攔截器 -->
		<jaxws:inInterceptors>
			<ref bean="inIntercepter"/>
		</jaxws:inInterceptors>
		<jaxws:outInterceptors>
			<ref bean="outIntercepter"/>
		</jaxws:outInterceptors>
	</jaxws:server>
	<!-- 配置攔截器的bean -->
	<bean name="inIntercepter" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
	<bean name="outIntercepter" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
	
	<!-- 配置服務實現類 -->
	<bean name="weatherInterface" class="cn.itcast.ws.cxf.server.WeatherInterfaceImpl"/>
</beans>

客戶端

開發步驟:

第一步:引入jar包

第二步:生成客戶端程式碼


第三步:配置spring配置檔案,applicationContent.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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
				            http://www.springframework.org/schema/beans/spring-beans.xsd
				            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
				            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
				            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
	<!-- <jaxws:client實現客戶端 ,對JaxWsProxyFactoryBean類封裝-->	
	<jaxws:client id="weatherClient" address="http://127.0.0.1:8080/ws_2_cxf_spring_server/ws/weather" serviceClass="cn.itcast.cxf.weather.WeatherInterface"/>
</beans>

第四步:從spring上下文中獲取服務實現類

第五步:呼叫查詢方法,列印

package cn.itcast.cxf.client;

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

import cn.itcast.cxf.weather.WeatherInterface;

public class WeatherClient {

	public static void main(String[] args) {
		//初始化spring的上下文
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		WeatherInterface  weatherInterface = (WeatherInterface) context.getBean("weatherClient");
		String weather = weatherInterface.queryWeather("保定");
		System.out.println(weather);
	}
}





相關推薦

webservice--CXF+Spring整合釋出SOAP協議服務

CXF+Spring整合釋出SOAP協議的服務 服務端 開發步驟: 第一步:建立web專案(引入jar包) 第二步:建立SEI介面 package cn.itcast.ws.cxf.server;

axis2+spring整合釋出webservice服務

1.環境搭建,準備相應環境jar包    axis2-1.6.0.0下載地址: http://axis.apache.org/axis2/java/core/download.cgi    spring相關jar包下載地址

CXF系列之JAX-RS:CXFspring整合釋出REST服務

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transition

spring boot 整合WebService Cxf簡單例項(soap

這裡是一個簡單的例子服務端pom檔案如下<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schema

WebService學習之旅(三)JAX-WS與Spring整合釋出WebService

Spring本身就提供了對JAX-WS的支援,有興趣的讀者可以研究下Spring的Spring-WS專案,專案地址: http://docs.spring.io/spring-ws/sites/1.5/downloads/releases.html 基於Sp

CXF Spring 整合 包 的問題

Error creating bean with name 'orderWS': Invocation of init method failed; nested exception is java.lang.LinkageError: 正在從引導類載入器載入 JAXB 2.

WebService-CXF-Spring 基於web的cxf(三)------參考傳智播客視訊

1      WebService-CXF-Spring 基於web的cxf 1.1    開發cxf的web專案:--原始碼eclipse無法訪問請使用myeclipse l  由於cxf的web專案已經集成了Spring所以,cxf的服務類都是在spring的配置檔

CXF+Spring+JAXB+Json構建Restful服務

話不多說,先看具體的例子: 檔案目錄結構:          web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmln

WebService+CXF+Spring練習之手機號碼歸屬地查詢

基礎知識簡述:WebService:WebService是使用http傳輸SOAP協議資料的一種遠端呼叫技術,跨防火牆(防火牆預設對http協議不攔截),跨平臺(使用XML封裝資料),支援面向物件。WS

WebServiceCXF框架與Spring整合釋出服務入門

1、建立WEB專案,將CXF與SPRING的包引入專案(在下載的CXF的lib包裡,裡面已經整合好了,直接引入進去就行了) 2、建立介面 package com.ckinghan.cxf.server.service; import javax.jws

CXF + Spring 釋出 SOAP服務

web工程目錄如下:  實體類com.cc.entity.User如下: package com.cc.entity; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable;

CXF實現簡單webservice應用、整合spring釋出到tomcat

前言 Apache CXF提供了用於方便地構建和開發WebService的可靠基礎架構。它允許建立高效能和可擴充套件的服務,可以部署在Tomcat和基於spring的輕量級容器中,也可以部署在更高階的伺服器上,例如Jboss、WebSphere或WebLogic。下面將建立一個簡單的we

WebServiceCXF整合Spring實現介面釋出和呼叫過程

CXF整合Spring實現介面釋出 釋出過程如下: 1、引入jar包(基於maven管理) <dependency> <groupId>org.apache.cxf</groupId> <artifactId>

Idea釋出Axis2服務端、建立WebService客戶端和spring整合Axis2框架釋出服務

一、使用預設的WEBSERVICE服務 1 新建專案 2 選擇 java->Java EE(由J2EE 8改為J2EE 7的版本,解決建立WEBSERVICE專案無WEB.XML的問題) 3 建立WEBSERVICE服務端 4 專案結構如下 5 生成wsd

Spring整合cxf搭建webservice【一、服務端】

1、匯入jar包(普通web專案) 引入依賴(maven專案) <!-- webservice依賴框架CXF --> <dependency> <groupId>org.apache.cxf</groupId>

WebService Spring整合Jax-rs規範 使用CXF框架Restful的程式設計風格 編寫服務

前提是專案SSM框架搭建好。 搭建cxf框架, 一、首先,將框架所需要的jar匯入,pom.xml檔案中 <!-- cxf 進行rs開發 必須匯入 --> <dependency> <groupId>org.apache.cx

Spring 整合 Apache CXF釋出webService

建立web專案,或使用maven建立webapp Maven建立web專案後,自動新增jar包: Pom.xml中還需要新增的jar包配置: <dependency> <groupId>org.apache.cxf</groupId&

WebService--CXFSpring整合(jaxws:endpoint形式配置)

tid archetype 全路徑 systems hide onf -o hot conf 一、CXF與Spring整合(jaxws:endpoint形式配置) 工具要點:idea、maven 1.新建一個maven項目 <?xml version="1.0"

CXF+Spring+Hibernate實現RESTful webservice服務端實例

fast anti vax apach sql xsd txadvice component path 1.RESTful API接口定義 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * *

WebServiceCXF整合Spring實現接口發布和調用過程2

creat tco win [] exception onf del tac xml配置 一、CXF整合Spring實現接口發布 發布過程如下: 1、引入jar包(基於maven管理) <!-- cxf --> <dependency>