Title: xfire-based Web Service Construction
Source: Xiaoqiang's home
Time: Sun, 12 Apr 2009 12:30:29 + 0000
Author: xiaojiit
Address: www.sharejava.cn/blog/read.php/12.htm
Content:
Here, xfire1.2.6 and spring 2.0 are used. myeclispe is used as the development tool. Although myeclipse comes with the xfire plug-in for development, the following manual creation is required to not rely on the development tool.
Xfire can be well integrated with spring. The following two sections describe xfire.
Pure xfire Web Service
1.1. first create a web project, open the downloaded xfire package, copy all jar packages under lib and all jar packages under the modules directory to the WEB-INF/lib directory of the web project.
1.2 modify the Web. xml file and add the following content to support xfire:
<Servlet>
<Servlet-Name> xfireservlet </servlet-Name>
<Servlet-class> org. codehaus. xfire. Transport. http. xfirepolicableservlet
</Servlet-class>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfireservlet </servlet-Name>
<URL-pattern>/services/* </url-pattern>
</Servlet-mapping>
In this way, you can access all published web services through http: // localhost: 8080/projectname/services/path.
1.3. Create the WEB-INF/xfire/directory under the META-INF directory under the project, and create the services. xml file under this Directory, which is used to add my web services to be released. The file content is as follows:
<Beans>
<Service xmlns = "http://xfire.codehaus.org/config/1.0">
<Name> helloservice </Name>
<Serviceclass> com. Services. helloservice </serviceclass> <implementationclass> com. Services. helloserviceimpl </implementationclass>
</Service>
</Beans>
The file content is as follows:
: Publish a web service;
: Name of the published web service;
: Interface class for providing services;
: A specific class that implements the service interface class.
Note: Spring 2.0 is used here. If spring1.2.6 is built with xfire, xmlns = "http://xfire.codehaus.org/config/1.0" should be put in to define.
1.4. Publish the WEB Project to Tomcat.
Http: // localhost: 8080/xfire/services/helloservice? WSDL
To call this web service.
In spring integrated xfire development web services, here is spring 2.0, if spring2.5, you need to add the spring-webmvc.jar in.
The following describes the xfire development steps integrated with spring.
Spring and xfire can be combined in three ways. The following describes how to directly integrate with spring.
(Via org. springframework. Web. servlet. dispatcherservlet of spring ).
2.1 put the xfire and spring jar packages to the classpath of the web Project
2.2 modify web. XML to support spring and xfire. Add the following content:
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>
// WEB-INF/CONF/applicationcontext. xml
Classpath: ORG/codehaus/xfire/spring/xfire. xml
</Param-value>
</Context-param>
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
<Servlet>
<Servlet-Name> xfire </servlet-Name>
<Servlet-class>
Org. springframework. Web. servlet. dispatcherservlet
</Servlet-class>
<Load-on-startup> 0 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfire </servlet-Name>
<URL-pattern>/service/* </url-pattern>
</Servlet-mapping>
2.3 create the spring configuration file applicationcontext. XML based on the content of web. xml.
In this example, place the file in the WEB-INF/conf directory. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<Bean id = "hellobean" class = "com. Service. helloserviceimpl"/>
</Beans>
A bean is defined here, and the Implementation class of the Web Services interface is used here.
2.4. Create a WEB-INF file under the xfire-servlet.xml. The xfire here is the name of the servlet org. springframework. Web. servlet. dispatcherservlet, which should be modified according to your own
The content is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<! -- Write this name here, so that you can use the bean defined in applicationcontext. xml -->
<Import resource = "classpath: ORG/codehaus/xfire/spring/xfire. xml"/>
<! -- This is the definition of URL ing -->
<Bean class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping">
<Property name = "urlmap">
<Map>
<! -- Write the URL ing of each web service here -->
<Entry key = "/helloservice"> // URL, access address
<Ref bean = "hello"/>
</Entry>
</Map>
</Property>
</Bean>
<! -- Define a basic service, and the following service references it -->
<Bean id = "basewebservice"
Class = "org. codehaus. xfire. Spring. remoting. xfireexporter"
Lazy-init = "false" abstract = "true">
<! -- Reference the factory defined in xfire. xml -->
<Property name = "servicefactory" ref = "xfire. servicefactory"/>
<! -- Reference xfire instance xfire in xfire. XML is the name of dispatcherservlet configured in Web. xml -->
<Property name = "xfire" ref = "xfire"/>
</Bean>
<! -- Define a hello WebService. Here serviceclass is the interface -->
<Bean id = "hello" parent = "basewebservice">
<Property name = "servicebean">
<Ref bean = "hellobean"/>
</Property>
<Property name = "serviceclass">
<Value> com. Service. helloservice </value>
</Property>
</Bean>
</Beans>
In this way, a Web Service is generated, which can be accessed through the domain name and the client code is compiled.
In another way, use the org. codehaus. xfire. Spring. xfirespringservlet class that comes with xfire for settings.
The method is as follows:
Modify web. xml and add the following content:
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/applicationcontext. xml </param-value>
</Context-param>
<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>
<! -- Xfire configuration -->
<Servlet>
<Servlet-Name> xfireservlet </servlet-Name>
<Servlet-class>
Org. codehaus. xfire. Spring. xfirespringservlet
</Servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfireservlet </servlet-Name>
<URL-pattern>/service/* </url-pattern>
</Servlet-mapping>
In this way, you don't have to use the xfire-servlet.xml file to add the Web Service bean directly in applicationcontext. xml. The applicationcontext. XML Code is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<! -- Web service configuration -->
<Import resource = "classpath: ORG/codehaus/xfire/spring/xfire. xml"/>
<Bean id = "helloserviceimpl"
Class = "com. Service. impl. helloserviceimpl"/>
<Bean id = "helloservice"
Class = "org. codehaus. xfire. Spring. remoting. xfireexporter">
<Property name = "xfire" ref = "xfire"/>
<Property name = "servicebean" ref = "helloserviceimpl"/>
<! -- Implement bean of WebService -->
<Property name = "serviceclass"
Value = "com. Service. Service. helloservice"/>
</Bean>
</Beans>
However, there is no security for a web service. Anyone who obtains the WSDL can easily construct a client program to access our web service.