码迷,mamicode.com
首页 > 编程语言 > 详细

spring集成cxf发布restful和soap接口

时间:2016-01-19 17:22:11      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

对于SOAP Webservice和Restful Webservice的选择问题,首先需要理解就是SOAP偏向于面向活动,有严格的规范和标准,包括安全,事务等各个方面的内容,同时SOAP强调操作 方法和操作对象的分离,有WSDL文件规范和XSD文件分别对其定义。而REST强调面向资源,只要我们要操作的对象可以抽象为资源即可以使用REST架 构风格。

application-context-cxf.xml

<?xml version="1.0" encoding="UTF-8"?>                         
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.1.xsd
                        http://cxf.apache.org/jaxws 
                        http://cxf.apache.org/schemas/jaxws.xsd
                        http://cxf.apache.org/jaxrs
                        http://cxf.apache.org/schemas/jaxrs.xsd">
                        
     <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    
    <bean id="SpringContextUtil" class="com.sharp.slc.common.SpringContextUtil" />

    <!-- DeviceManager Service -->
    <!-- 
    <jaxws:endpoint id="devicemanager"
        implementor="com.sharp.slc.agent.purifier.webservice.impl.DeviceManagerServiceImpl"
        address="/DeviceManagerService" />
    -->
    <bean id="agentService"
        class="com.sharp.slc.agent.purifier.webservice.impl.AgentServiceImpl"/>
    
    <jaxws:server id="agentSOAPService" 
        serviceClass="com.sharp.slc.agent.purifier.webservice.impl.AgentServiceImpl" 
        address="/agentSOAPService">
        <jaxws:serviceBean>
            <ref bean="agentService"/>
        </jaxws:serviceBean>
    </jaxws:server>
    
    <bean id="agentServiceForTcp"
        class="com.sharp.slc.agent.purifier.webservice.impl.AgentServiceForTcpImpl"/>
    
    <jaxws:server id="agentSOAPServiceForTcp" 
        serviceClass="com.sharp.slc.agent.purifier.webservice.impl.AgentServiceForTcpImpl" 
        address="/agentSOAPServiceForTcp">
        <jaxws:serviceBean>
            <ref bean="agentServiceForTcp"/>
        </jaxws:serviceBean>
    </jaxws:server>
 
     <!-- HMS Server Service 
    <jaxws:endpoint id="hms"
        implementor="com.sharp.slc.agent.purifier.webservice.impl.HMSServiceImpl"
        address="/HMSService" />
        -->

    <!--  agent CXF RESTful Publish -->     
    <jaxrs:server id="agentServiceContainer" address="/agentWebService">
        <jaxrs:serviceBeans>
            <ref bean="agentWebBean" />
        </jaxrs:serviceBeans>
        <jaxrs:extensionMappings>
            <entry key="json" value="application/json" />
            <entry key="xml" value="application/xml" />
        </jaxrs:extensionMappings>
        <jaxrs:languageMappings>
               <entry key="en" value="en-gb"/>  
        </jaxrs:languageMappings>
    </jaxrs:server>
    
    <bean id="agentWebBean"
        class="com.webservice.impl.ReceiveHMSDataImpl"/>

    <!-- WeChat client -->

    <!-- 
    <jaxws:client id="wechatClient" 
        serviceClass="com.sharp.slc.wechat.webservice.WeChatInterface" 
        address="http://localhost:8080/SharpCloudWeb/webservice/weChatService"/>
     -->
         
    <jaxws:client id="wechatClient" 
        serviceClass="com.sharp.slc.wechat.webservice.WeChatInterface" 
        address="${wechat.endpoint}"/>
    
    <jaxws:client id="tcpClient" 
        serviceClass="com.sharp.slc.net.webservice.ITCPService" 
        address="${tcpserver.endpoint}"/>

    <!-- 
    <bean id="weChat" class="com.sharp.slc.wechat.webservice.WeChat"
        factory-bean="weChatProxyFactory" factory-method="create" />

    <bean id="socketProxyFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.sharp.slc.net.webservice.Socket" />
        <property name="address"
            value="http://localhost:8080/sharp-slc-net/SocketService" />
    </bean>
    -->
    
    
</beans>

 

 IReceiveHMSData.java

package com.webservice;

import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("/receiveHMSServer")
@WebService
public interface IReceiveHMSData {
    
    /**
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @POST
    @Path("/notifyDeviceModification")
    @Produces({MediaType.APPLICATION_JSON })
    String notifyDeviceModification(@Context HttpServletRequest servletRequest, @Context HttpServletResponse servletResponse);
 }

 ReceiveHMSDataImpl.java

package com.webservice.impl;

@WebService(endpointInterface = "com.sharp.slc.sharpcloud.webservice.IReceiveHMSData")
public class ReceiveHMSDataImpl implements IReceiveHMSData {

	@POST
	@Path("/notifyDeviceModification")
	@Produces({ MediaType.APPLICATION_JSON })
	public String notifyDeviceModification(@Context HttpServletRequest request, @Context HttpServletResponse response) {
		return "";
	}
}

 soap的接口代码

package com.sharp.slc.wechat.webservice;

import java.util.List;

import javax.jws.WebService;

@WebService
public interface WeChatInterface {
	/**
	 * 根据macAddress获取设备信息
	 * 
	 * @param openId
	 * @return
	 */
	public void sendConnStatusToWeChat(List<String> openIdList, String deviceId, String status);

	/**
	 * 根据macAddress获取设备信息
	 * 
	 * @param openIdList
	 * @param message
	 */
	public void sendTextMessageToWeChat(List<String> openIdList, String message);

	/**
	 * 根据macAddress获取设备信息
	 * 
	 * @param openId
	 * @param deviceStatus
	 * @return
	 */
	public void sendDeviceStatusToWeChat(String openId, String deviceStatus);

	/**
	 * 发送绑定成功信息
	 * 
	 * @param openId
	 * @param deviceId
	 * @return
	 */
	public void sendBindSuccessToWeChat(String openId, String deviceId);
}

 

spring集成cxf发布restful和soap接口

标签:

原文地址:http://www.cnblogs.com/codeydt/p/5142548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!