webservice ------ CXF集成到spring中,并发布服务,默认支持soap1.1,如需soap1.2增加注解

1.集成jar包

aopalliance-1.0.jar
asm-3.3.1.jar
commons-logging-1.2.jar
cxf-core-3.0.1.jar
cxf-rt-bindings-soap-3.0.1.jar
cxf-rt-bindings-xml-3.0.1.jar
cxf-rt-databinding-jaxb-3.0.1.jar
cxf-rt-frontend-jaxws-3.0.1.jar
cxf-rt-frontend-simple-3.0.1.jar
cxf-rt-transports-http-3.0.1.jar
cxf-rt-ws-addr-3.0.1.jar
cxf-rt-wsdl-3.0.1.jar
cxf-rt-ws-policy-3.0.1.jar
hamcrest-core-1.3.jar
jaxb-core-2.2.10-b140310.1920.jar
jaxb-impl-2.2.10-b140310.1920.jar
junit-4.12.jar
neethi-3.0.3.jar
spring-aop-4.1.7.RELEASE.jar
spring-beans-4.1.7.RELEASE.jar
spring-context-4.1.7.RELEASE.jar
spring-core-4.1.7.RELEASE.jar
spring-expression-4.1.7.RELEASE.jar
spring-test-4.1.7.RELEASE.jar
spring-web-4.1.7.RELEASE.jar
stax2-api-3.1.4.jar
woodstox-core-asl-4.4.0.jar
wsdl4j-1.6.3.jar
xml-resolver-1.2.jar
xmlschema-core-2.1.0.jar

2.配置web.xml Servlet 运行在 Web 服务器或应用服务器上的程序

<servlet>
		<servlet-name>CXFService</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>CXFService</servlet-name>
		<url-pattern>/cxfservices/*
	

3.配置applicationContext.xml 文件

<import resource="springxml/applicationContext-cxf.xml"/>

4.添加applicationContext-cxf.xml专用于cxf注册服务

<?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"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<!-- 监管记录 -->
	<jaxws:server id="recordcheckaction" address="/recordcheckaction" serviceClass="com.hanweb.jgxt.publish.server.record.RecordCheckAction">
	<jaxws:server id="recordcompany" address="/recordcompany" serviceClass="com.hanweb.jgxt.publish.server.record.RecordCompany">
		<jaxws:serviceBean>
			<bean class="com.hanweb.jgxt.publish.server.record.impl.RecordCompanyImpl" />
		</jaxws:serviceBean>
	</jaxws:server>
</beans>

5.增加发布接口类 (注意默认支持soap1.1,如果支持soap1.2 则增加注解
支持soap1.2 增加注解

@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_MTOM_BINDING)
package com.hanweb.jgxt.publish.server.record;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.BindingType;

@WebService(targetNamespace = "http://server.record.recordcompany.com/")
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_MTOM_BINDING)
public interface RecordCompany {
    /**
     *    获取该公司信息
     * @param data 文本信息 --- companyCode
     * @param token 认证token
     * @return 返回基本信息
     */
    @WebMethod(action = "http://server.record.recordcompany.com/getCompanyInfo")
    public String getCompanyInfo(@WebParam(name="data") String data, @WebParam(name="token")  String token, @WebParam(name="vssid") String vssid);
}

6.增加发布接口实现类

package com.hanweb.jgxt.publish.server.record.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.jws.WebService;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hanweb.jgxt.publish.interfaces.RecordDao;
import com.hanweb.jgxt.publish.pojo.NodeResult;
import com.hanweb.jgxt.publish.server.record.RecordCompany;

@WebService(endpointInterface = "com.hanweb.jgxt.publish.server.record.RecordCompany", serviceName = "recordcompany")
public class RecordCompanyImpl implements RecordCompany{

    @Resource(name = "recordDao")
    private RecordDao recordDao;
    /**
     *    获取该公司信息
     * @param data 文本信息 --- companyCode
     * @param token 认证token
     * @return 返回基本信息
     */
    public String getCompanyInfo(String data, String token, String vssid) {
        System.out.println("getCompanyInfo接收参数data:=============="+data);
        System.out.println("getCompanyInfo接收参数token:=============="+token);
        System.out.println("getCompanyInfo接收参数vssid:=============="+vssid);
        JSONObject jb = JSONObject.parseObject(data);
        NodeResult node = new NodeResult();
        JSONObject jo = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String time = sdf.format(new Date());
        try {
            String companyInfo = recordDao.getCompanyBasicInfo(jb.getString("companyCode"));
            jo.put("companyInfo", companyInfo);
            String checkAction = recordDao.getCheckAction(jb.getString("companyCode"));
            jo.put("checkAction", checkAction);
            String punishAction = recordDao.getPunishAction(jb.getString("companyCode"));
            jo.put("punishAction", punishAction);
            String forceAction = recordDao.getForceAction(jb.getString("companyCode"));
            jo.put("forceAction", forceAction);
            String otherAction = recordDao.getOtherAction(jb.getString("companyCode"));
            jo.put("otherAction", otherAction);
            node.setCode("200");
            node.setMessage(JSON.toJSONString(jo));
        } catch (Exception e) {
            node.setCode("500");
            node.setMessage("服务器异常!");
            e.printStackTrace();
        }
        node.setTime(time);
        System.out.println("返回参数:==================="+node.toString());
        String xml = ""+node.getCode()+"" + 
                  node.getMessage() + "";
        return xml;
    }
}

你可能感兴趣的:(webservice)