Spring整合CXF
程序员文章站
2022-07-13 11:17:20
...
1. Spring整合CXF之发布WebService服务
CXF官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-spring.html
2. Spring整合CXF之添加拦截器
CXF官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-spring.html
用Spring来整合CXF,来发布WebService服务; New Maven Project -> maven-archetype-webapp 1.0 Group Id: com.andrew.webservice Artfact Id: WS_Spring_CXF Version:0.0.1-SNAPSHOT Packaging:jar
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.andrew.webservice</groupId> <artifactId>WS_Spring_CXF</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>WS_Spring_CXF Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- 添加Spring支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.1.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.1.7.RELEASE</version> </dependency> <!-- 添加cxf支持 --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.5</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.5</version> </dependency> </dependencies> <build> <finalName>WS_Spring_CXF</finalName> </build> </project> 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>WS_Spring_CXF</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> <!-- Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- Spring监听器 --> <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> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping> </web-app> MapAdapter.java package com.andrew.adapter; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import com.andrew.entity.MyRole; import com.andrew.entity.Role; public class MapAdapter extends XmlAdapter<MyRole[], Map<String, List<Role>>> { @Override public Map<String, List<Role>> unmarshal(MyRole[] v) throws Exception { Map<String, List<Role>> map = new HashMap<String, List<Role>>(); for (int i = 0; i < v.length; i++) { MyRole r = v[i]; map.put(r.getKey(), r.getValue()); } return map; } @Override public MyRole[] marshal(Map<String, List<Role>> v) throws Exception { MyRole[] roles = new MyRole[v.size()]; int i = 0; for (String key : v.keySet()) { roles[i] = new MyRole(); roles[i].setKey(key); roles[i].setValue(v.get(key)); i++; } return roles; } } MyRole.java package com.andrew.entity; import java.util.List; public class MyRole { private String key; private List<Role> value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public List<Role> getValue() { return value; } public void setValue(List<Role> value) { this.value = value; } } Role.java package com.andrew.entity; public class Role { private Integer id; private String roleName; public Role() { super(); } public Role(Integer id, String roleName) { super(); this.id = id; this.roleName = roleName; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } } User.java package com.andrew.entity; public class User { private Integer id; private String userName; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } MyInterceptor.java package com.andrew.interceptor; import java.util.List; import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.headers.Header; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.phase.AbstractPhaseInterceptor; import org.apache.cxf.phase.Phase; import org.w3c.dom.Element; import org.w3c.dom.NodeList; public class MyInterceptor extends AbstractPhaseInterceptor<SoapMessage> { public MyInterceptor() { super(Phase.PRE_INVOKE); } @SuppressWarnings("null") public void handleMessage(SoapMessage message) throws Fault { List<Header> headers = message.getHeaders(); if (headers == null && headers.size() == 0) { throw new Fault(new IllegalArgumentException("没有Header,拦截器实施拦截")); } Header firstHeader = headers.get(0); Element ele = (Element) firstHeader.getObject(); NodeList uList = ele.getElementsByTagName("userName"); NodeList pList = ele.getElementsByTagName("password"); if (uList.getLength() != 1) { throw new Fault(new IllegalArgumentException("用户名格式不对")); } if (pList.getLength() != 1) { throw new Fault(new IllegalArgumentException("密码格式不对")); } String userName = uList.item(0).getTextContent(); String password = pList.item(0).getTextContent(); if (!userName.equals("andrew") || !password.equals("123456")) { throw new Fault(new IllegalArgumentException("用户名或者密码错误!")); } } } HelloWorld.java package com.andrew.webservice; import java.util.List; import java.util.Map; import javax.jws.WebService; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.andrew.adapter.MapAdapter; import com.andrew.entity.Role; import com.andrew.entity.User; @WebService public interface HelloWorld { public String say(String str); public List<Role> getRoleByUser(User user); @XmlJavaTypeAdapter(MapAdapter.class) public Map<String,List<Role>> getRoles(); } HelloWorldImpl.java package com.andrew.webservice.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.jws.WebService; import org.springframework.stereotype.Component; import com.andrew.entity.Role; import com.andrew.entity.User; import com.andrew.webservice.HelloWorld; @Component("helloWorld") @WebService public class HelloWorldImpl implements HelloWorld { public String say(String str) { return "Hello:" + str; } public List<Role> getRoleByUser(User user) { List<Role> roleList = new ArrayList<Role>(); if (user != null) { if (user.getUserName().equals("andrew") && user.getPassword().equals("123456")) { roleList.add(new Role(1, "经理")); roleList.add(new Role(2, "组长")); } else if (user.getUserName().equals("tony") && user.getPassword().equals("123456")) { roleList.add(new Role(3, "员工")); } return roleList; } else { return null; } } public Map<String, List<Role>> getRoles() { Map<String, List<Role>> map = new HashMap<String, List<Role>>(); List<Role> roleList1 = new ArrayList<Role>(); roleList1.add(new Role(1, "经理")); roleList1.add(new Role(2, "组长")); map.put("andrew", roleList1); List<Role> roleList2 = new ArrayList<Role>(); roleList2.add(new Role(3, "员工")); map.put("jack", roleList2); return map; } } 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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.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:component-scan base-package="com.andrew.webservice" /> <!-- 定义服务提供者 --> <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld"></jaxws:endpoint> </beans> http://localhost:8080/WS_Spring_CXF/webservice/ http://localhost:8080/WS_Spring_CXF/webservice/HelloWorld?wsdl
2. Spring整合CXF之添加拦截器
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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.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:component-scan base-package="com.andrew.webservice" /> <!-- 定义服务提供者 --> <jaxws:endpoint implementor="#helloWorld" address="/HelloWorld"> <!-- 添加in拦截器 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean class="com.andrew.interceptor.MyInterceptor"/> </jaxws:inInterceptors> <!-- 添加out拦截器 --> <jaxws:outInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> </jaxws:outInterceptors> </jaxws:endpoint> </beans>
上一篇: maven初识
下一篇: HTTP Keep-Alive详解[转]