Spring3.2 + cxf1.7.3整合
參考:[url]http://tsinglongwu.iteye.com/blog/832704[/url]
[color=red][b]測試除錯工具介紹 [/b][/color]
[b]1.SoapUI1.6[/b] [url]http://webservices.ctocio.com.cn/tips/263/7817763.shtml[/url]
[b]2.TestMaker[/b] [url]http://webservices.ctocio.com.cn/tips/263/7817763_2.shtml[/url]
[b]3.WebServiceStudio[/b] [url]http://demo.tc/Post/529[/url]
SoapUI支援eclipse外掛
pom.xml
[color=red][b]提供服務端[/b][/color]
web.xml
非註解:applicationContext.xml
[b]<jaxws:endpoint>有三個屬性id, implementor和address。[/b]
“[color=red][b]id[/b][/color]”指定這個Bean在Spring上下文中唯一的標識。
“[color=red][b]implementor[/b][/color]”指定了這個Web Service的實現類。
“[color=red][b]address[/b][/color]”指定了服務在Web伺服器上釋出的地址。這個地址可以包含ip和埠的完整地址,也可以是隻包含相對路徑的地址。
註解:applicationContext.xml
[b]注意:[/b] 同時應該在實現類加上@Component註解.
介面:使用@WebService註解,表明這個介面就是對外提供的服務.
實現類:使用@WebService(endpointInterface = "com.webservices.IHelloWorld", serviceName = "HelloWorld")註解,表明這個是IHelloWorld服務的實現類,並提供釋出到外部,服務名字是HelloWorld
也可以使用@Component去註解實現類,才能使用註解方式.
執行工程,進入:[url]http://localhost:8080/webservice/HelloWorld?wsdl[/url] ,得到的wsdl
則表示服務端的webservice已經部署成功,就等著客戶端來訪問.
[color=red][b]客戶端:[/b][/color]
pom.xml, [b]類似服務端的pom.xml,只是名字有差別而已[/b]
使用命令得到基本的類:
[color=darkblue][b]wsdl2java -d /mnt/d/vmware_shared_folder/workspace/webserviceclient/src/main/java -p com.websercices.client -frontend jaxws21 http://localhost:8080/webservice/HelloWorld?wsdl[/b][/color]
applicationContext.xml
[color=red][b]注意:[/b][/color]
1. org.apache.cxf.jaxws.JaxWsProxyFactoryBean要知道提供服務的介面和URL.
2. <property name="address" value="http://localhost:8080/webservice/HelloWorld" />中的/HelloWorld要與伺服器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address屬性對應。
建立一個測試類:
執行客戶端測試類,得到結果: Hello 豬頭!
[color=red][b]測試除錯工具介紹 [/b][/color]
[b]1.SoapUI1.6[/b] [url]http://webservices.ctocio.com.cn/tips/263/7817763.shtml[/url]
[b]2.TestMaker[/b] [url]http://webservices.ctocio.com.cn/tips/263/7817763_2.shtml[/url]
[b]3.WebServiceStudio[/b] [url]http://demo.tc/Post/529[/url]
SoapUI支援eclipse外掛
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pandy</groupId>
<artifactId>webservice</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webservice Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.1.RELEASE</spring.version>
<tomcat.version>2.1-SNAPSHOT</tomcat.version>
<junit.version>3.8.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring jars -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-asm</artifactId>
<version>3.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<!-- CXF jars -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>webservice</finalName>
</build>
</project>
[color=red][b]提供服務端[/b][/color]
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<context-param>
<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>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
非註解: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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 心版本去掉cxf/cxf-extension-soap.xml-->
<!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->
<bean id="hello" class="com.webservices.impl.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello"
address="/HelloWorld" />
</beans>
[b]<jaxws:endpoint>有三個屬性id, implementor和address。[/b]
“[color=red][b]id[/b][/color]”指定這個Bean在Spring上下文中唯一的標識。
“[color=red][b]implementor[/b][/color]”指定了這個Web Service的實現類。
“[color=red][b]address[/b][/color]”指定了服務在Web伺服器上釋出的地址。這個地址可以包含ip和埠的完整地址,也可以是隻包含相對路徑的地址。
註解: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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
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
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<context:annotation-config />
<context:component-scan base-package="com" />
<jaxws:endpoint id="helloWorld" implementor="#helloWorldImpl"
address="/HelloWorld" />
</beans>
[b]注意:[/b] 同時應該在實現類加上@Component註解.
介面:使用@WebService註解,表明這個介面就是對外提供的服務.
package com.webservices;
import javax.jws.WebParam;
import javax.jws.WebService;
import com.webservices.bean.User;
/**
* 將要用來發布的介面
* @author pandy
*
*/
@SuppressWarnings("restriction")
@WebService
public interface IHelloWorld {
// 加入WebParam註解,以保證xml檔案中引數名字的正確性
String sayHi(@WebParam(name = "text") String text);
String sayHiToUser(User user);
}
實現類:使用@WebService(endpointInterface = "com.webservices.IHelloWorld", serviceName = "HelloWorld")註解,表明這個是IHelloWorld服務的實現類,並提供釋出到外部,服務名字是HelloWorld
package com.webservices.impl;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.jws.WebService;
import com.webservices.IHelloWorld;
import com.webservices.bean.User;
/**
* 介面的實現類
* @author pandy
*
*/
@SuppressWarnings({ "restriction" })
@WebService(endpointInterface = "com.webservices.IHelloWorld", serviceName = "HelloWorld")
public class HelloWorldImpl implements IHelloWorld {
Map<Integer, User> users = new LinkedHashMap<Integer, User>();
@Override
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
@Override
public String sayHiToUser(User user) {
System.out.println("sayHiToUser called");
users.put(users.size() + 1, user);
return "Hello " + user.getName();
}
}
也可以使用@Component去註解實現類,才能使用註解方式.
執行工程,進入:[url]http://localhost:8080/webservice/HelloWorld?wsdl[/url] ,得到的wsdl
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.webservices.com/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http"
xmlns:ns1="http://webservices.com/" name="HelloWorld" targetNamespace="http://impl.webservices.com/">
<wsdl:import
location="http://localhost:8080/webservice/HelloWorld?wsdl=IHelloWorld.wsdl"
namespace="http://webservices.com/"></wsdl:import>
<wsdl:binding name="HelloWorldSoapBinding" type="ns1:IHelloWorld">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayHi">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHi">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHiResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="sayHiToUser">
<soap:operation soapAction="" style="document" />
<wsdl:input name="sayHiToUser">
<soap:body use="literal" />
</wsdl:input>
<wsdl:output name="sayHiToUserResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:port binding="tns:HelloWorldSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:8080/webservice/HelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
則表示服務端的webservice已經部署成功,就等著客戶端來訪問.
[color=red][b]客戶端:[/b][/color]
pom.xml, [b]類似服務端的pom.xml,只是名字有差別而已[/b]
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.pandy</groupId>
<artifactId>webserviceclient</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>webserviceclient Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>3.2.1.RELEASE</spring.version>
<tomcat.version>2.1-SNAPSHOT</tomcat.version>
<junit.version>3.8.1</junit.version>
</properties>
<dependencies>
......
</dependencies>
<build>
<finalName>webserviceclient</finalName>
</build>
</project>
使用命令得到基本的類:
[color=darkblue][b]wsdl2java -d /mnt/d/vmware_shared_folder/workspace/webserviceclient/src/main/java -p com.websercices.client -frontend jaxws21 http://localhost:8080/webservice/HelloWorld?wsdl[/b][/color]
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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="com.websercices.client.IHelloWorld" factory-bean="clientFactory"
factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.websercices.client.IHelloWorld" />
<property name="address" value="http://localhost:8080/webservice/HelloWorld" />
</bean>
</beans>
[color=red][b]注意:[/b][/color]
1. org.apache.cxf.jaxws.JaxWsProxyFactoryBean要知道提供服務的介面和URL.
2. <property name="address" value="http://localhost:8080/webservice/HelloWorld" />中的/HelloWorld要與伺服器端applicationContext.xml中的<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />的address屬性對應。
建立一個測試類:
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.websercices.client.IHelloWorld;
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-client.xml");
IHelloWorld server = (IHelloWorld) ctx.getBean("client");
String result = server.sayHi("豬頭!");
System.out.println(result);
}
}
執行客戶端測試類,得到結果: Hello 豬頭!
相關文章
- spring3.2 @Scheduled註解 定時任務Spring
- 整合持續整合工具
- 【springboot】學習4:整合JDBC、整合druid、整合mybatis、整合 SpringSecuritySpring BootJDBCUIMyBatisGse
- SpringBoot整合系列-整合JPASpring Boot
- MyBatis(九) 整合Spring、整合SpringMVCMyBatisSpringMVC
- SSM整合之CRUD環境搭建整合SSM
- ssm整合SSM
- SpringBoot整合系列-整合H2Spring Boot
- RestCloud iPaaS混合整合平臺,資料整合RESTCloud
- SpringBoot整合系列–整合MyBatis-plusSpring BootMyBatis
- 整合學習(一):簡述整合學習
- SSH框架整合配置所需JAR包(SSH整合)框架JAR
- 自動化整合:Pipeline整合Docker容器Docker
- ETL資料整合,RestCloud資料整合平臺RESTCloud
- Win10整合Cortana,Mac何時整合Siri?Win10Mac
- Spring AI與大模型Ollama如何整合整合?SpringAI大模型
- Vue整合UeditorVue
- SSM框架整合SSM框架
- Vuex之整合Vue
- CodePush整合
- 整合SSM框架SSM框架
- Spark整合hiveSparkHive
- Vitis AI 整合AI
- SSH框架整合框架
- OpenStack整合DockerDocker
- QTP整合SikuliQT
- 系統整合
- 整合SwiftLintSwift
- 整合學習
- SSM學習筆記3——整合 SpringMVC、整合SSMSSM筆記SpringMVC
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- API整合新一代平臺,iPaaS整合平臺API
- 企業如何資料整合?資料整合解決方案
- 五個模型整合模型
- Spring 整合 MyBatisSpringMyBatis
- SSM整合開發SSM
- SpringMVC整合MybatisSpringMVCMyBatis
- 資料庫整合資料庫