CXF开发webservice实例

Apache CXF 是一个开放源代码框架。在几个主流框架中,性能最佳,学习成本比axis2要低很多。这是性能测试的文章链接。http://blog.csdn.net/chenleixing/article/details/44958549

不多说,直接上代码,配置web.xml

<!-- 告知javaEE容器,有哪些内容需要添加到上下文中去 -->
 <context-param>
      <param-name>contextConfigLocation</param-name>
	<param-value>
		classpath:cxf-applicationContext.xml
	</param-value>
</context-param>
<servlet><servlet-name>mcs-servlet</servlet-name>

<servlet-class>org.apache.cxf.transport.servlet.CXFServlet </servlet-class>

<load-on-startup>101</load-on-startup></servlet><servlet-mapping>

<servlet-name>mcs-servlet</servlet-name>

<url-pattern>/webservice/*</url-pattern>

</servlet-mapping>



 
 

cxf-application.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:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context"
       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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com.develop.ssm.modules.rsport.webservice.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>
    <context:component-scan base-package="com.develop.ssm.modules.rsport.webservice.*">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>

    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <!--  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> -->
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

    <jaxws:endpoint
            id="webOperator"
            implementor="com.develop.ssm.modules.rsport.webservice.impl.RsWebInterfaceImpl"
            address="/rsWebInterface" />

    <bean id="client" class="com.develop.ssm.modules.rsport.webservice.RsWebInterface"
          factory-bean="clientFactory" factory-method="create"/>

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="com.develop.ssm.modules.rsport.webservice.RsWebInterface"/>
        <property name="address" value="http://localhost:8080/webservice/rsWebInterface"/>
    </bean>


</beans>

java接口代码

package com.develop.ssm.modules.rsport.webservice;

import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

/**
 * Created by hezhan01 on 2017/6/19.
 */
@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface RsWebInterface {
    public String webInvoke(@WebParam(name = "param") String param) throws Exception;
    public void test();

}

java实现类代码

@WebService(endpointInterface = "com.develop.ssm.modules.rsport.webservice.RsWebInterface", serviceName = "rsWebInterface")
public class RsWebInterfaceImpl implements RsWebInterface{

    @Resource
    private WebServiceContext context;
    @Override
    public String webInvoke(String param) throws Exception{
        //设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dateStr = sdf.format(date);
        //获取当前项目的路径
        String path=Thread.currentThread().getContextClassLoader().getResource("").getPath();
        //去掉路径前面的斜杠
        path = path.substring(1);
        sdf = new SimpleDateFormat("yyyyMMdd");
        path = path+"logs";
        File file = new File(path);
        //判断文件夹是否存在
        if(!file.exists()&&!file.isDirectory()){
            //创建文件夹
            file.mkdir();
        }
        //每天一个文件
        path = path + "/log_"+sdf.format(date)+".txt";
        File writename = new File(path);
        //判断文件是否存在
        if(!writename.exists()){
            // 创建新文件
            writename.createNewFile();
        }
        //设置成追加内容模式 true
        FileWriter fw = new FileWriter(writename,true);
        PrintWriter out = new PrintWriter(fw);
        //写入内容
        out.println("时间:" + dateStr + " webservice-入参内容:" + param );
        // 把缓存区内容压入文件
        out.flush();
        fw.flush();
        // 最后关闭文件
        out.close();
        fw.close();
        return param;
    }
    public void test(){

    }
}




 

猜你喜欢

转载自blog.csdn.net/qiye_zhou/article/details/73803651