一、使用axis用wsdl生成Webservice:
工具:有axis插件的eclipse,wsdl文件;
操作步骤:
新建工程--》选择wsdl文件--》右键选择Webservice--->Generate Java Bean Skeleton
为了自动生成server-config.wsdd文件 需要勾选上图所示地方;
<ns1:deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" xmlns:ns1="http://xml.apache.org/axis/wsdd/"> <ns1:globalConfiguration> <ns1:parameter name="sendMultiRefs" value="true"/> <ns1:parameter name="disablePrettyXML" value="true"/> <ns1:parameter name="adminPassword" value="admin"/> <ns1:parameter name="attachments.Directory" value="F:workspace_study.metadata.pluginsorg.eclipse.wst.server.core mp1wtpwebappshtmltestWEB-INFattachments"/> <ns1:parameter name="dotNetSoapEncFix" value="true"/> <ns1:parameter name="enableNamespacePrefixOptimization" value="false"/> <ns1:parameter name="sendXMLDeclaration" value="true"/> <ns1:parameter name="sendXsiTypes" value="true"/> <ns1:parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <ns1:requestFlow> <ns1:handler type="java:org.apache.axis.handlers.JWSHandler"> <ns1:parameter name="scope" value="session"/> </ns1:handler> <ns1:handler type="java:org.apache.axis.handlers.JWSHandler"> <ns1:parameter name="scope" value="request"/> <ns1:parameter name="extension" value=".jwr"/> </ns1:handler> </ns1:requestFlow> </ns1:globalConfiguration> <ns1:handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <ns1:handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <ns1:handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <ns1:service name="AdminService" provider="java:MSG"> <ns1:parameter name="allowedMethods" value="AdminService"/> <ns1:parameter name="enableRemoteAdmin" value="false"/> <ns1:parameter name="className" value="org.apache.axis.utils.Admin"/> <ns1:namespace>http://xml.apache.org/axis/wsdd/</ns1:namespace> </ns1:service> <ns1:service name="Version" provider="java:RPC"> <ns1:parameter name="allowedMethods" value="getVersion"/> <ns1:parameter name="className" value="org.apache.axis.Version"/> </ns1:service> <ns1:service name="ImportMaterialServiceHttpPort" provider="java:RPC" style="document" use="literal"> <ns1:parameter name="allowedMethods" value="*"/> <ns1:parameter name="typeMappingVersion" value="1.2"/> <ns1:parameter name="wsdlPortType" value="ImportMaterial"/> <ns1:parameter name="className" value="com.dayang.ADP.service.ImportMaterialService._0_1.ImportMaterialHttpBindingSkeleton"/> <ns1:parameter name="wsdlServicePort" value="ImportMaterialServiceHttpPort"/> <ns1:parameter name="schemaQualified" value="http://dayang.com/ADP/schema/ADFileInfoEntity/1.0,http://dayang.com/ADP/schema/ADBroadcastListEntity/1.0,http://dayang.com/ADP/schema/ADPServiceParameterType/0.1,http://dayang.com/ADP/schema/ADMaterialEntity/1.0"/> <ns1:parameter name="wsdlTargetNamespace" value="http://dayang.com/ADP/service/ImportMaterialService/0.1"/> <ns1:parameter name="wsdlServiceElement" value="ImportMaterialService"/> <ns2:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns1:CommonRequestType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADPServiceParameterType._0_1.CommonRequestType" xmlns:ns1="http://dayang.com/ADP/schema/ADPServiceParameterType/0.1" xmlns:ns2="http://xml.apache.org/axis/wsdd/"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns2:ImportMaterialRequestType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADPServiceParameterType._0_1.ImportMaterialRequestType" xmlns:ns2="http://dayang.com/ADP/schema/ADPServiceParameterType/0.1"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns3:ADMaterialEntityType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADMaterialEntity._1_0.ADMaterialEntityType" xmlns:ns3="http://dayang.com/ADP/schema/ADMaterialEntity/1.0"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns4:MaterialServiceParameterResponseType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADPServiceParameterType._0_1.MaterialServiceParameterResponseType" xmlns:ns4="http://dayang.com/ADP/schema/ADPServiceParameterType/0.1"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns5:ExtendAttributeType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADMaterialEntity._1_0.ExtendAttributeType" xmlns:ns5="http://dayang.com/ADP/schema/ADMaterialEntity/1.0"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns6:CommonResponseType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADPServiceParameterType._0_1.CommonResponseType" xmlns:ns6="http://dayang.com/ADP/schema/ADPServiceParameterType/0.1"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns7:ExtendAttributeType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.dayang.ADP.schema.ADPServiceParameterType._0_1.ExtendAttributeType" xmlns:ns7="http://dayang.com/ADP/schema/ADPServiceParameterType/0.1"/> <ns1:arrayMapping innerType="ns8:ExtendAttributeType" languageSpecificType="java:com.dayang.ADP.schema.ADMaterialEntity._1_0.ExtendAttributeType[]" qname="ns8:ExtendAttributesType" xmlns:ns8="http://dayang.com/ADP/schema/ADMaterialEntity/1.0"/> <ns1:arrayMapping innerType="ns9:ExtendAttributeType" languageSpecificType="java:com.dayang.ADP.schema.ADPServiceParameterType._0_1.ExtendAttributeType[]" qname="ns9:ExtendAttributesType" xmlns:ns9="http://dayang.com/ADP/schema/ADPServiceParameterType/0.1"/> </ns1:service> <ns1:service name="ImportProgramListServiceHttpPort" provider="java:RPC" style="document" use="literal"> <ns1:parameter name="allowedMethods" value="*"/> <ns1:parameter name="typeMappingVersion" value="1.2"/> <ns1:parameter name="wsdlPortType" value="ImportProgramList"/> <ns1:parameter name="className" value="MREML2.schema.ImportProgramListService.ImportProgramListHttpBindingSkeleton"/> <ns1:parameter name="wsdlServicePort" value="ImportProgramListServiceHttpPort"/> <ns1:parameter name="schemaQualified" value="http://MREML2/schema/ProgramListParameter,http://MREML2/schema/CommonDataType,http://MREML2/schema/ProgramList"/> <ns1:parameter name="wsdlTargetNamespace" value="http://MREML2/schema/ImportProgramListService"/> <ns1:parameter name="wsdlServiceElement" value="ImportProgramListService"/> <ns2:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns1:ImportProgramListRequestType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramListParameter.ImportProgramListRequestType" xmlns:ns1="http://MREML2/schema/ProgramListParameter" xmlns:ns2="http://xml.apache.org/axis/wsdd/"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns2:ColumnInfoType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.ColumnInfoType" xmlns:ns2="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns3:BroadcastListEntityType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.BroadcastListEntityType" xmlns:ns3="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns4:ExecuteActionType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.ExecuteActionType" xmlns:ns4="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns5:BroadcastListItemType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.BroadcastListItemType" xmlns:ns5="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns6:ProgramInfoType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.ProgramInfoType" xmlns:ns6="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns7:SignalSourceType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.SignalSourceType" xmlns:ns7="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns8:CommonRequestType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.CommonDataType.CommonRequestType" xmlns:ns8="http://MREML2/schema/CommonDataType"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns9:TapeInfoType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.TapeInfoType" xmlns:ns9="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns10:ImportProgramListResponseType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramListParameter.ImportProgramListResponseType" xmlns:ns10="http://MREML2/schema/ProgramListParameter"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns11:ExtendAttributeType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.CommonDataType.ExtendAttributeType" xmlns:ns11="http://MREML2/schema/CommonDataType"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns12:ChannelInfoType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.ProgramList.ChannelInfoType" xmlns:ns12="http://MREML2/schema/ProgramList"/> <ns1:typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="" qname="ns13:CommonResponseType" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:MREML2.schema.CommonDataType.CommonResponseType" xmlns:ns13="http://MREML2/schema/CommonDataType"/> <ns1:arrayMapping innerType="ns14:ExtendAttributeType" languageSpecificType="java:MREML2.schema.CommonDataType.ExtendAttributeType[]" qname="ns14:ExtendAttributesType" xmlns:ns14="http://MREML2/schema/CommonDataType"/> </ns1:service> <ns1:transport name="http"> <ns1:requestFlow> <ns1:handler type="URLMapper"/> <ns1:handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </ns1:requestFlow> <ns1:parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/> <ns1:parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> <ns1:parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/> <ns1:parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/> <ns1:parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/> <ns1:parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> </ns1:transport> <ns1:transport name="local"> <ns1:responseFlow> <ns1:handler type="LocalResponder"/> </ns1:responseFlow> </ns1:transport> </ns1:deployment>
Web.xml中会相应生成相关代码:
<servlet> <display-name>Apache-Axis Servlet</display-name> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet> <display-name>Axis Admin Servlet</display-name> <servlet-name>AdminServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AdminServlet</servlet-name> <url-pattern>/servlet/AdminServlet</url-pattern> </servlet-mapping>
启动工程,访问http:ip:port/name/services,发布成功;
二、使用cxf集成springmvc发布Webservice:
工具:cxf-2.4.1 eclipse wsdl spingmvc
使用cxf中的wsdl2java命令生成java文件:
wsdl2java用法:
wsdl2java -p com -d src -all aa.wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名:
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
生成的java文件导入到工程中,添加spring依赖,依赖文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd"> <info organisation="" module="adagent" status="integration"> </info> <configurations> <conf name="default" visibility="public" description="runtime dependencies and master artifact can be used with this conf" extends="runtime,master"/> <conf name="master" visibility="public" description="contains only the artifact published by this module itself, with no transitive dependencies"/> <conf name="compile" visibility="public" description="this is the default scope, used if none is specified. Compile dependencies are available in all classpaths."/> <conf name="provided" visibility="public" description="this is much like compile, but indicates you expect the JDK or a container to provide it. It is only available on the compilation classpath, and is not transitive."/> <conf name="runtime" visibility="public" description="this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath." extends="compile"/> <conf name="test" visibility="private" description="this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases." extends="runtime"/> <conf name="system" visibility="public" description="this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository."/> <conf name="sources" visibility="public" description="this configuration contains the source artifact of this module, if any."/> <conf name="javadoc" visibility="public" description="this configuration contains the javadoc artifact of this module, if any."/> <conf name="optional" visibility="public" description="contains all optional dependencies"/> </configurations> <dependencies> <!--spring--> <dependency org="org.springframework" name="spring-core" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-beans" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-web" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-context" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-context-support" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-aop" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-expression" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-tx" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-jms" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-jdbc" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework" name="spring-webmvc" rev="4.0.4.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.slf4j" name="slf4j-api" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.slf4j" name="jcl-over-slf4j" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.slf4j" name="jul-to-slf4j" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.slf4j" name="log4j-over-slf4j" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="ch.qos.logback" name="logback-classic" rev="1.1.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="ch.qos.logback" name="logback-core" rev="1.1.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <!--json序列化--> <dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.3.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.3.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.3.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <!--HTTP--> <dependency org="org.aopalliance" name="com.springsource.org.aopalliance" rev="1.0.0" /> <dependency org="org.springframework.data" name="spring-data-commons" rev="1.10.0.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.httpcomponents" name="httpclient" rev="4.3.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.httpcomponents" name="httpcore" rev="4.3.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.httpcomponents" name="httpmime" rev="4.3.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.ws.xmlschema" name="xmlschema-core" rev="2.1.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <!--cxf--> <dependency org="org.apache.cxf" name="cxf-bundle" rev="2.7.11" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.neethi" name="neethi" rev="3.0.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.velocity" name="velocity" rev="1.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.ws.xmlschema" name="xmlschema-core" rev="2.1.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="javax.xml.bind" name="jaxb-api" rev="2.2.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="javax.xml.soap" name="saaj-api" rev="1.3.5" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="com.sun.xml.bind" name="jaxb-xjc" rev="2.2.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="com.sun.xml.bind" name="jaxb-core" rev="2.2.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="com.sun.xml.messaging.saaj" name="saaj-impl" rev="1.3.23" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.codehaus.woodstox" name="woodstox-core-asl" rev="4.3.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.codehaus.woodstox" name="stax2-api" rev="3.1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.jvnet.staxex" name="stax-ex" rev="1.7.7" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.codehaus.jettison" name="jettison" rev="1.3.5" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="wsdl4j" name="wsdl4j" rev="1.6.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <!-- axis相关内容 --> <dependency org="axis" name="axis" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" /> <dependency org="axis" name="axis-jaxrpc" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" /> <dependency org="axis" name="axis-saaj" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" /> <dependency org="axis" name="axis-wsdl4j" rev="1.5.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" /> <dependency org="commons-discovery" name="commons-discovery" rev="0.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" /> <dependency org="com.google.code.gson" name="gson" rev="2.3" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="dom4j" name="dom4j" rev="1.6.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <!-- <dependency org="org.apache.axis" name="axis" rev="1.4" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> --> <dependency org="jaxen" name="jaxen" rev="1.1-beta-9" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="javax.validation" name="validation-api" rev="1.1.0.Final" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false" /> <dependency org="javax.ws.rs" name="javax.ws.rs-api" rev="2.0-m10" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.springframework.data" name="spring-data-mongodb" rev="1.7.0.RELEASE" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.mongodb" name="mongo-java-driver" rev="3.0.0-rc1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.solr" name="solr-solrj" rev="5.1.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="org.apache.commons" name="commons-vfs2" rev="2.0" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="commons-discovery" name="commons-discovery" rev="0.5" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="commons-logging" name="commons-logging" rev="1.2" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> <dependency org="javax.xml.rpc" name="javax.xml.rpc-api" rev="1.1" conf="compile->compile(*),master(*);runtime->runtime(*)" transitive="false"/> </dependencies> </ivy-module>
Web.xml文件添加代码:
<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>/*</url-pattern> </servlet-mapping>
spring的beans文件添加如下代码:
<bean id="WebServiceBean" class="org.tempuri.WebserviceSoapImpl"></bean> <jaxws:server id="WebServiceSoap" serviceClass="org.tempuri.WebServiceSoap" address="/WebService.asmx"> <jaxws:serviceBean> <ref bean="WebServiceBean"/> </jaxws:serviceBean> </jaxws:server>
以上可实现webservice的发布;
三、使用cxf发布restful接口:
添加代码:
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
@Component("cxfRestfulRS")
public class CxfRestfulRS {
private static final Logger logger = LoggerFactory.getLogger(CxfRestfulRS.class);
@Autowired
MongoTemplate mongoTemplate;
@GET
@Path(value = "/check")
public String check() {
try {
System.out.println("服务运行正常...");
} catch (Throwable e) {
logger.error(e.getMessage());
logger.debug(e.getMessage(), e);
}
return "服务运行正常...";
}
@Path(value = "/add")
@GET
public void add() {
try {
TestMo mo = new TestMo();
mo.setId("1111");
mo.setMessage("2222");
mo.setName("3333");
mongoTemplate.insert(mo);
} catch (Throwable e) {
logger.error(e.getMessage());
logger.debug(e.getMessage(), e);
}
}
}
添加beans配置:
<jaxrs:server id="webService" address="/"> <!--输入拦截器设置--> <jaxrs:inInterceptors> </jaxrs:inInterceptors> <!--输出拦截器设置--> <jaxrs:outInterceptors> </jaxrs:outInterceptors> <!--serviceBeans:暴露的WebService服务类--> <jaxrs:serviceBeans> <ref bean="cxfRestfulRS"/> </jaxrs:serviceBeans> <!--支持的协议--> <jaxrs:extensionMappings> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> </jaxrs:extensionMappings> </jaxrs:server>
beans.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:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.5.xsd http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- 自动扫描 --> <context:annotation-config /> <mvc:annotation-driven /> <context:component-scan base-package="com.ad" /> <context:property-placeholder location="classpath:com/ad/mongodb.properties" /> <!-- <mongo:mongo id="mongo" host="${mongodb.host}" port="${mongodb.port}" /> --> <mongo:mongo id="mongo" replica-set="${mongodb.replicaSet}" /> <mongo:db-factory id="mongoDbFactory" dbname="${mongodb.database}" mongo-ref="mongo" /> <!-- mongo模板操作对象 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </bean> <bean id="WebServiceBean" class="org.tempuri.WebserviceSoapImpl"></bean> <jaxws:server id="WebServiceSoap" serviceClass="org.tempuri.WebServiceSoap" address="/WebService.asmx"> <jaxws:serviceBean> <ref bean="WebServiceBean"/> </jaxws:serviceBean> </jaxws:server> <jaxrs:server id="webService" address="/"> <!--输入拦截器设置--> <jaxrs:inInterceptors> </jaxrs:inInterceptors> <!--输出拦截器设置--> <jaxrs:outInterceptors> </jaxrs:outInterceptors> <!--serviceBeans:暴露的WebService服务类--> <jaxrs:serviceBeans> <ref bean="cxfRestfulRS"/> </jaxrs:serviceBeans> <!--支持的协议--> <jaxrs:extensionMappings> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> </jaxrs:extensionMappings> </jaxrs:server> </beans>
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_3_0.xsd" version="3.0"> <display-name>ADAgent</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-beans.xml</param-value> </context-param> <context-param> <param-name>logConfigFile</param-name> <param-value>config/logback.xml</param-value> </context-param> <context-param> <param-name>propertiesFiles</param-name> <param-value>config/adagent.properties</param-value> </context-param> <listener> <listener-class>com.ad.main.SpringWebMain</listener-class> </listener> <!-- <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/ad/beans.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> --> <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>/*</url-pattern> </servlet-mapping> <servlet> <display-name>Apache-Axis Servlet</display-name> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping> <servlet> <display-name>Axis Admin Servlet</display-name> <servlet-name>AdminServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AdminServlet</servlet-class> <load-on-startup>100</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AdminServlet</servlet-name> <url-pattern>/servlet/AdminServlet</url-pattern> </servlet-mapping> </web-app>
package com.ad.main; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.util.StringUtils; import org.springframework.web.context.ContextLoaderListener; import com.LoggerConfigurator; public class SpringWebMain extends ContextLoaderListener { protected static final String INIT_PARAM_HOMEDIR = "homeDirectory"; protected static final String INIT_PARAM_LOGCONFIGFILE = "logConfigFile"; protected static final String INIT_PARAM_PROPERTIESFILES = "propertiesFiles"; protected static final String SYS_PPT_APPHOME = "app.home"; protected static final Logger logger = LoggerFactory .getLogger(SpringWebMain.class); public static <T> T getBean(Class<T> requiredType) { ApplicationContext ac = ContextLoaderListener .getCurrentWebApplicationContext(); if (ac != null) { return ac.getBean(requiredType); } else { throw new org.springframework.context.ApplicationContextException( "ApplicationContext has not been set."); } } public static <T> T getBean(String name, Class<T> requiredType) { ApplicationContext ac = ContextLoaderListener .getCurrentWebApplicationContext(); if (ac != null) { return ac.getBean(name, requiredType); } else { throw new org.springframework.context.ApplicationContextException( "ApplicationContext has not been set."); } } @Override public void contextDestroyed(ServletContextEvent event) { org.slf4j.bridge.SLF4JBridgeHandler.uninstall(); super.contextDestroyed(event); } @Override public void contextInitialized(ServletContextEvent event) { ServletContext sc = event.getServletContext(); initialize(sc); super.contextInitialized(event); } protected String getLogbackConfigFileName() { return null; } protected String[] getPropertiesFileNames() { return new String[] {}; } private String getLogbackConfigFileName(ServletContext servletContext) { String cfn = servletContext.getInitParameter(INIT_PARAM_LOGCONFIGFILE); if (StringUtils.hasLength(cfn)) { return cfn; } else { return this.getLogbackConfigFileName(); } } private String[] getPropertiesFileNames(ServletContext servletContext) { String ppfs = servletContext .getInitParameter(INIT_PARAM_PROPERTIESFILES); if (StringUtils.hasLength(ppfs)) { return ppfs.split(";"); } else { return this.getPropertiesFileNames(); } } protected void initialize(ServletContext servletContext) { String appname = servletContext.getServletContextName(); String homedir_ppt_name = appname + ".home"; String homedir = servletContext.getInitParameter(INIT_PARAM_HOMEDIR); if (!StringUtils.hasText(homedir)) { homedir = System.getProperty(homedir_ppt_name); } if (!StringUtils.hasText(homedir)) { homedir = System.getenv(appname + "_home"); } if (!StringUtils.hasText(homedir)) { homedir = "."; logger.warn("未找到" + homedir_ppt_name + ",请正确配置,否则系统将无法正常运行!"); logger.warn("暂时将" + homedir_ppt_name + "的值设置为当前目录!"); } // 强制加入System,以便logback等其他配置使用 System.setProperty(homedir_ppt_name, homedir); System.setProperty(SYS_PPT_APPHOME, homedir); // 日志处理 System.setProperty("org.apache.cxf.Logger", "org.apache.cxf.common.logging.Slf4jLogger"); if (!org.slf4j.bridge.SLF4JBridgeHandler.isInstalled()) { org.slf4j.bridge.SLF4JBridgeHandler.install(); } File logcf = new File(homedir, getLogbackConfigFileName(servletContext)); logger.info("加载日志配置文件:{}", logcf.getAbsolutePath()); LoggerConfigurator.config(logcf.getAbsolutePath()); // 加载配置文件 for (String str : getPropertiesFileNames(servletContext)) { if (StringUtils.hasLength(str)) { loadProperties(homedir + "/" + str); } } } private void loadProperties(String filename) { File file = new File(filename); if (!file.exists()) { logger.warn("未找到文件:" + filename + ",请正确配置,否则系统将无法正常运行!"); return; } logger.info("加载配置文件:{}", file.getAbsolutePath()); Properties ppts = new Properties(); try { ppts.load(new FileReader(file)); } catch (FileNotFoundException e) { logger.error("未找到文件:" + filename + ",请正确配置,否则系统将无法正常运行!"); } catch (IOException e) { logger.error("加载文件:" + filename + ",请正确配置,否则系统将无法正常运行!"); } // 插入到System.Properties for (Enumeration<?> e = ppts.propertyNames(); e.hasMoreElements();) { String key = (String) e.nextElement(); System.setProperty(key, ppts.getProperty(key)); } } }
package com; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.core.util.StatusPrinter; public class LoggerConfigurator { public static void config(String logbackCfg) { LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory(); try { JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); // the context was probably already configured by default // configuration rules lc.reset(); configurator.doConfigure(logbackCfg); } catch (JoranException je) { // StatusPrinter will handle this } StatusPrinter.printIfErrorsOccured(lc); } }