• Spring整合CXF之发布WebService服务


    建立一个maven的Web项目。

    1.pom.xml

     <!-- 添加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>

    2.index.jsp

    <html>
    <body>
    <h2>Hello World!</h2>
    </body>
    </html>

    3.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>

    4.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.java1234.webservice" />
        
        <!-- 定义服务提供者  -->
        <jaxws:endpoint
            implementor="#helloWorld"
            address="/HelloWorld">
             <!-- 添加in拦截器 -->
            <jaxws:inInterceptors>
                 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
                 <bean class="com.java1234.interceptor.MyInterceptor"/>
            </jaxws:inInterceptors>
            <!-- 添加out拦截器 -->
            <jaxws:outInterceptors>
                 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
            </jaxws:outInterceptors>
         </jaxws:endpoint>
    </beans>

    5.HelloWorld

    @WebService
    public interface HelloWorld {
    
        public String say(String str);
        
        public List<Role> getRoleByUser(User user);
        
        /**
         * 获取所有用户以及对应的角色
         * @return
         */
        @XmlJavaTypeAdapter(MapAdapter.class)
        public Map<String,List<Role>> getRoles();
    }

    6.HelloWorldImpl

    @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("java1234") && user.getPassword().equals("123456")){
                    roleList.add(new Role(1,"技术总监"));
                    roleList.add(new Role(2,"架构师"));
                }else if(user.getUserName().equals("jack") && 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("java1234", roleList1);
            
            List<Role> roleList2=new ArrayList<Role>();
            roleList2.add(new Role(3,"程序员"));
            map.put("jack", roleList2);
            
            return map;
        }
    
    }

    7.自定义拦截器

    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("java1234")||!password.equals("123456")){
                throw new Fault(new IllegalArgumentException("用户名或者密码错误!"));
            }
        }
    
    }

    8.MapAdapter

    public class MapAdapter extends XmlAdapter<MyRole[], Map<String,List<Role>>>{
    
        /**
         * 适配转换 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;
        }
    
        /**
         * 适配转换 Map<String, List<Role>> -> MyRole[]
         */
        @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;
        }
    
    }

    9.MyRole,Role,User

    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;
        }
        
        
    }
    
    public class Role {
    
        private Integer id; // 编号
        private String roleName; // 角色名称
        
        
        
        public Role() {
            super();
            // TODO Auto-generated constructor stub
        }
        
        
        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;
        }
        
        
    }
    
    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;
        }
        
        
    }

    运行程序,访问:

    http://localhost:8080/webservice/HelloWorld?wsdl

    结果:

    已经成功发布WebService。

    代码来自视频

  • 相关阅读:
    回调函数、Java接口回调 总结
    java 中hashcode和equals 总结
    单一职责原则
    Java初始化理解与总结 转载
    2019-2020-2 20175305张天钰《网络对抗技术》Exp9 Web安全基础
    2019-2020-2 20175305张天钰《网络对抗技术》Exp 8 Web基础
    2019-2020-2 20175305 张天钰《网络对抗技术》Exp7 网络欺诈防范
    2019-2020-2 20175305张天钰《网络对抗技术》Exp6 MSF基础应用
    2019-2020-2 20175305张天钰《网络对抗技术》Exp5 信息搜集与漏洞扫描
    2019-2020-2 20175305张天钰《网络对抗技术》 Exp4 恶意代码分析
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/9570223.html
Copyright © 2020-2023  润新知