`
ConeyLiu
  • 浏览: 127861 次
  • 性别: Icon_minigender_2
  • 来自: 重庆
社区版块
存档分类
最新评论

ssh项目上添加webservice

阅读更多

最近一个ssh的项目需要添加webservice接口的。

就这个配置捣鼓了半天,下面在这里记录一下具体的流程方便下次使用哦。

我的环境是:

 MyEclipse6.5+ tomcat6 +jdk1.6

 

刚开始在网站找了一个篇webservice项目的例子。试着做了一下。

这个项目是在创建项目的时候就添加了webservice。

参考例子:

http://www.cnblogs.com/simle/archive/2011/10/31/2230091.html

 

下面记录一下我是我现有的ssh项目添加的webservice

 

步骤一:

        在项目上面加载webservice库  点击 项目右键 → myeclipse → add xfire webservice Capabilities 一直下一步,完成。

步骤二:

        在web.xml里面添加XFire的配置

<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/xfire-servlet.xml</param-value>

  </context-param>

 

 

 <!-- begin XFire 配置 -->
    <servlet>  
       <servlet-name>xfire</servlet-name>  
       <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>  
    <servlet-mapping>
       <servlet-name>xfire</servlet-name>
       <url-pattern>*.ws</url-pattern>
    </servlet-mapping>
     <servlet-mapping>
	  <servlet-name>xfire</servlet-name>
	  <url-pattern>/services/*</url-pattern>
	 </servlet-mapping>
  <servlet>
       <!-- 配合Spring容器中XFire一起工作的Servlet-->
       <servlet-name>xfireServlet</servlet-name>
       <servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
    </servlet>
    <servlet-mapping>
       <servlet-name>xfireServlet</servlet-name>
       <!-- 在这个URI下开放Web Service服务 -->
       <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
<!-- end XFire 配置 -->

 

这里里配置的时候得注意一点。在加载webservice库的时候会自动增加一些配置在这里面。注意看一下。这里是和spring相配置的。把他丢给spring管理。

 

步骤三:

         在WEB-INF下面新建xfire-servlet.xml ,在步骤二里面已经引用到了。。。

我的xfire-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <!-- For configuring Authentication with XFire and Acegi, please see:
         http://jroller.com/page/sqyuan?entry=using_acegi_for_authentication_authorization -->
    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

    <!-- If you want to use JAXB instead of XmlBeans, you'll need to add the following dependency
         to your pom.xml.
         <dependency>
            <groupId>org.codehaus.xfire</groupId>
            <artifactId>xfire-jaxb2</artifactId>
            <version>${xfire.version}</version>
        </dependency> -->
    
    <!--<bean id="jaxbTypeMappingRegistry" class="org.codehaus.xfire.jaxb2.JaxbTypeRegistry"
          init-method="createDefaultMappings"/>-->

    <bean id="xmlbeansTypeRegistry" class="org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry"/>
    
    <bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>

    <bean id="handlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
        <!--<property name="typeMappingRegistry" ref="jaxbTypeMappingRegistry"/>-->
        <property name="typeMappingRegistry" ref="xmlbeansTypeRegistry"/>
        <property name="xfire" ref="xfire"/>
        <property name="webAnnotations" ref="webAnnotations"/>
    </bean>

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="urlMap">
            <map>
                <entry key="/">
                    <ref bean="handlerMapping"/>
                </entry>
            </map>
        </property>
    </bean>
</beans> 

 

 步骤四:

       在src下面 新建一个包 cn.service ,在包里面建一个借口一个类试一下

接口IWebService .java:

package cn.service;

import javax.jws.WebService;

//Generated by MyEclipse
@WebService
public interface IWebService {
	
	public String example(String message);
	public String  test(String s );
}

 

实现类:

package cn.service;

import javax.jws.WebService;

//Generated by MyEclipse
@WebService(serviceName = "service789", endpointInterface = "cn.service.IWebService")
public class WebServiceImpl implements IWebService {
	
	public String example(String message) {
		return message;
	}

	public String  test(String s ){
		String n = "this's ";
		String t = n+s;
		return t;
	}
}

 

在这里得注意接口上面和实现类上面的注解。

 

步骤五:

       交给了spring来管理。就在spring 的配置文件配置bean

<bean id="service123" class="cn.service.WebServiceImpl">
       
</bean>

 

步骤六:

      启动tomcat访问 这里访问路径得注意,访问bean下面配置的name路径会自动跳转到实现类注解上面的serviceName。

 

步骤七:

       错误提示。

启动tomcat的的时候会报错:

错误一:

 

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Line 8 in XML document from ServletContext resource [/WEB-INF/classes/applicationContext-common.xml] is invalid; nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
 at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)

 

这个错误是因为原本我ssh项目配置里面已经有一个spring包了。我在加载webservice的时候又给自动添加了一个spring包。两个包冲突所引起的。我这里 一个spring.jar  一个spring-1.2.6.jar  我是删掉了spring-1.2.6.jar这个包。

 

解决了刚才那个问题有出现了

 

错误二:

 

严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry] for bean with name 'xmlbeansTypeRegistry' defined in ServletContext resource [/WEB-INF/xfire-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.codehaus.xfire.xmlbeans.XmlBeansTypeRegistry

 这个错误提示是说缺少包。缺少了一个叫做xfire-xmlbeans-1.2.6.jar 的包。。

这个包在xfire-servlet.xml  配置文件里面用到了。

将这包添加进去后再次启动tomcat就ok了

我的访问路径是

http://localhost:8080/demo/services/  这样他会自动找到你配置的webservice

http://localhost:8080/demo/services/service123?wsdl  这里service123是bean里面配置的name

他们两个路径最终的目的就是访问到webservice的实现类里面去。

http://localhost:8080/smsmanage/services/service789?wsdl 这里的service789就是在实现类上面servicename

正确访问会在浏览器上面出现一下类似于xml 的格式的内容

 

<?xml version="1.0" encoding="UTF-8" ?> 
- <wsdl:definitions targetNamespace="http://service.cn" xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://service.cn" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <wsdl:types>
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://service.cn">
- <xsd:element name="test">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="testResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="example">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
- <xsd:element name="exampleResponse">
- <xsd:complexType>
- <xsd:sequence>
  <xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string" /> 
  </xsd:sequence>
  </xsd:complexType>
  </xsd:element>
  </xsd:schema>
  </wsdl:types>
- <wsdl:message name="exampleResponse">
  <wsdl:part name="parameters" element="tns:exampleResponse" /> 
  </wsdl:message>
- <wsdl:message name="exampleRequest">
  <wsdl:part name="parameters" element="tns:example" /> 
  </wsdl:message>
- <wsdl:message name="testResponse">
  <wsdl:part name="parameters" element="tns:testResponse" /> 
  </wsdl:message>
- <wsdl:message name="testRequest">
  <wsdl:part name="parameters" element="tns:test" /> 
  </wsdl:message>
- <wsdl:portType name="service789PortType">
- <wsdl:operation name="test">
  <wsdl:input name="testRequest" message="tns:testRequest" /> 
  <wsdl:output name="testResponse" message="tns:testResponse" /> 
  </wsdl:operation>
- <wsdl:operation name="example">
  <wsdl:input name="exampleRequest" message="tns:exampleRequest" /> 
  <wsdl:output name="exampleResponse" message="tns:exampleResponse" /> 
  </wsdl:operation>
  </wsdl:portType>
- <wsdl:binding name="service789HttpBinding" type="tns:service789PortType">
  <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 
- <wsdl:operation name="test">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="testRequest">
  <wsdlsoap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="testResponse">
  <wsdlsoap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="example">
  <wsdlsoap:operation soapAction="" /> 
- <wsdl:input name="exampleRequest">
  <wsdlsoap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output name="exampleResponse">
  <wsdlsoap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
  </wsdl:binding>
- <wsdl:service name="service789">
- <wsdl:port name="service789HttpPort" binding="tns:service789HttpBinding">
  <wsdlsoap:address location="http://localhost:8080/smsmanage/services/service789" /> 
  </wsdl:port>
  </wsdl:service>
  </wsdl:definitions>

   由于刚接触这个webservice。了解不是很深。。可能描述的也是有点糟糕哦。。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics