Tianyi 19 -- simple implementation of Java-xfirewebservice and spring Integration

Source: Internet
Author: User

Declaration: Although I have integrated spring, I have not set up a connection pool and JDBC Connection database configuration. If necessary, I can configure it myself.


Xfire: xfire is a new generation of Java Web service engine. xfire makes it easy to publish Web Services in Java EE applications. Compared with other Web service engines, xfire is easy to configure and can be easily integrated with spring, which allows Java developers to achieve the same development efficiency as. NET developers.

WebService: Web services are online application services released by enterprises to meet their specific business needs. Other companies or application software can access and use this online service over the Internet.

1) before the configuration file, introduce the xfire1.2 corelibraries file and xfire 1.2 HTTP client libraries files and the jar packages required by spring. Refer to the directory structure below.

Web. xml configuration

<? XML version = "1.0" encoding = "UTF-8"?> <Web-app version = "2.5" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <! -- Load applicationcontext when running the service. XML and xfire-servlet.xml profiles --> <context-param> <param-Name> contextconfiglocation </param-Name> <param-value>/WEB-INF/applicationcontext. XML/WEB-INF/xfire-servlet.xml </param-value> </context-param> <! -- Load springcontextservlet at startup --> <listener-class> Org. springframework. web. context. contextloaderlistener </listener-class> </listener> <listener-class> Org. springframework. web. util. introspectorcleanuplistener </listener-class> </listener> <! -- End spring configuration --> <! -- Begin xfire configuration --> <servlet-Name> xfire </servlet-Name> <servlet-class> Org. springframework. web. servlet. dispatcherservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> xfire </servlet-Name> <URL-pattern> *. WS </url-pattern> </servlet-mapping> <servlet> <! -- Servlets that work with xfire in spring containers are essential --> <servlet-Name> xfireservlet </servlet-Name> <servlet-class> Org. codehaus. xfire. spring. xfirespringservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> xfireservlet </servlet-Name> <! -- Open the Web service under this URI --> <URL-pattern>/service/* </url-pattern> </servlet-mapping> <! -- End xfire configuration --> <welcome-file-List> <welcome-File> index. JSP </welcome-File> </welcome-file-List> </Web-app>

2) create a new WEB-INF file in the xfire-servlet.xml directory, the configuration is as follows:

<? 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" default-autowire = "byname"> <! -- Introduce xfire pre-configuration information --> <import resource = "classpath: ORG/codehaus/xfire/spring/xfire. xml"/> <! -- This step is essential and must be written --> <! -- Define the access URL --> <Bean class = "org. springframework. Web. servlet. handler. simpleurlhandlermapping"> <property name = "urlmap"> <map> <! -- Name of the service provided externally --> <Entry key = "/service. WS "> <ref bean =" service "/> </entry> </map> </property> </bean> <! -- Use the xfire export tool --> <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. xfire instance in XML --> <property name = "xfire" ref = "xfire"/> </bean> <bean id = "service" parent = "basewebservice"> <! -- Business service Bean --> <property name = "servicebean" ref = "mywebservice"/> <! -- Mywebservice here is mainly used in the applicationcontext. xml configuration file --> <! -- Narrow Interface Class of Business Service Bean --> <property name = "serviceclass" value = "com. Service. Support. ijavawebservice"/> </bean> </beans>

3) applicationcontext. xml configuration:

<? XML version = "1.0" encoding = "UTF-8"?> <Beansxmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: P = "http://www.springframework.org/schema/p" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <! -- Specifies the implementation class called by the program entry implementation class, the Dao or other objects declared in the implementation class are injected here --> <bean id = "mywebservice" class = "com. service. impl. javawebservice "> </bean> </beans>

2. Create an interface and an implementation class, that is, the ijavawebservice interface and javawebservice implementation class in the configuration file. The directory structure is shown in figure

:

1) interface code:

package com.service.support;public interface IJavaWebService {public String getMessage(String name,int age,String email); }

2) implementation class code:

package com.service.impl;import com.service.support.IJavaWebService;public class JavaWebService implements IJavaWebService {public String getMessage(String name, int age, String email) {// TODO Auto-generated method stubreturn name+" "+age+" "+email;}}

Iii. test whether the preceding configuration is successful:

1) release the newly created web project (I created the javawebservice project here) to the Tomcat server.

2) enter the following address in the browser address bar to test: http: // 192.168.0.104: 8081/javawebservice/service. ws? WSDL (replace the IP address with your local address). If the following figure is displayed, the configuration is successful!


4. Create a project for testing

1. The project directory structure is as follows:


2. Create an interface class. This interface class must be the same as the method in the interface class defined in the previous WebService project, including the number type of parameters and return values. The Code is as follows:

package com.ceshi.support;public interface ICeShiService {public String getMessage(String name,int age,String email); }

3. The project test code is as follows:

Package COM. ceshi. test; import java.net. malformedurlexception; import Org. codehaus. xfire. xfire; import Org. codehaus. xfire. xfirefactory; import Org. codehaus. xfire. client. xfireproxyfactory; import Org. codehaus. xfire. service. service; import Org. codehaus. xfire. service. binding. objectservicefactory; import COM. ceshi. support. iceshiservice; public class test {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubservice service = new objectservicefactory (). create (iceshiservice. class); // This class is the defined interface class // xfire = xfirefactory. newinstance (). getxfire (); xfireproxyfactory factory = new xfireproxyfactory (xfire); string url = "http: // 192.168.0.104: 8081/javawebservice/service. WS "; // the WebService address to be accessed, that is, add your local address and server port number to the name of the service provided by WebService in the xfire-servlet configuration file try {iceshiservice Ic = (iceshiservice) factory. create (Service, URL); string result = IC. getmessage ("name:" + "" + "" + "Age:", 22, "" + "E-mail:" + "wangzihu@hotmail.com"); system. out. println ("result ============>>>" + result);} catch (malformedurlexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

4. Run the main method. If the following screen is displayed, it indicates that the project has successfully called WebService:




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.