Spring整合CXF

阅读更多
1. Spring整合CXF之发布WebService服务

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


  4.0.0
  com.andrew.webservice
  WS_Spring_CXF
  war
  0.0.1-SNAPSHOT
  WS_Spring_CXF Maven Webapp
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
    
    
        org.springframework
        spring-core
        4.1.7.RELEASE
    
    
        org.springframework
        spring-beans
        4.1.7.RELEASE
    
    
         org.springframework
         spring-tx
         4.1.7.RELEASE
        
    
        org.springframework
        spring-context
        4.1.7.RELEASE
    
    
        org.springframework
        spring-context-support
        4.1.7.RELEASE
    
    
        org.springframework
        spring-web
        4.1.7.RELEASE
    
    
        org.springframework
        spring-webmvc
        4.1.7.RELEASE
    
    
        org.springframework
        spring-aop
        4.1.7.RELEASE
    
    
        org.springframework
        spring-aspects
        4.1.7.RELEASE
    
    
        org.springframework
        spring-jdbc
        4.1.7.RELEASE
    
    
    
        org.apache.cxf
        cxf-core
        3.1.5
    
    
        org.apache.cxf
        cxf-rt-frontend-jaxws
        3.1.5
    
    
        org.apache.cxf
        cxf-rt-transports-http
        3.1.5
    
  
  
    WS_Spring_CXF
  


web.xml



  WS_Spring_CXF
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  
  
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
     CXFServlet
     org.apache.cxf.transport.servlet.CXFServlet
    
    
       CXFServlet
       /webservice/*
    


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>> {

    @Override
    public Map> unmarshal(MyRole[] v) throws Exception {
        Map> map = new HashMap>();
        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> 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 value;
    
    public String getKey() {
        return key;
    }
    public void setKey(String key) {
        this.key = key;
    }
    public List getValue() {
        return value;
    }
    public void setValue(List 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 {
    public MyInterceptor() {
        super(Phase.PRE_INVOKE);
    }
    @SuppressWarnings("null")
    public void handleMessage(SoapMessage message) throws Fault {
        List
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 getRoleByUser(User user); @XmlJavaTypeAdapter(MapAdapter.class) public Map> 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 getRoleByUser(User user) { List roleList = new ArrayList(); 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> getRoles() { Map> map = new HashMap>(); List roleList1 = new ArrayList(); roleList1.add(new Role(1, "经理")); roleList1.add(new Role(2, "组长")); map.put("andrew", roleList1); List roleList2 = new ArrayList(); roleList2.add(new Role(3, "员工")); map.put("jack", roleList2); return map; } } applicationContext.xml http://localhost:8080/WS_Spring_CXF/webservice/ http://localhost:8080/WS_Spring_CXF/webservice/HelloWorld?wsdl


2. Spring整合CXF之添加拦截器

applicationContext.xml

    
    
    
    
    
    
    
    
    
    
    
        
        
             
             
        
        
        
             
        
    

你可能感兴趣的:(webservice)