Lesson Notes _web Service, an example of Xfire

Source: Internet
Author: User
Tags wsdl

Web Services Benefits

Interoperability: Implement mutual calls between different systems (language-independent, platform-independent)


What Web services are Web services is a class of applications that are able to programmatically implement a feature through a web call


Architecture of Web Services
Web services need to comply with the technical standard 1. XML (standard method for describing Web service data)
2. SOAP (the protocol representing Web Service information exchange)
3. WSDL (Web Service Description Language)
4. UDDI (Unified description Discovery and Integration)


Here about WebService not more detailed introduction, if interested can view Baidu Encyclopedia WebService, I here a collection: http://pan.baidu.com/s/1bnpLo43

two popular Web services open source frameworks in Java--comparison between axis and Xfire


Use an example of Web services to add Xfire User library click Tools window→preferences

Add jars adds a jar package and Xfire-all-1.2.6.jar under Lib;

Client invoke Web Service Training essentials:
Mastering the use of Xfire to create a client, invoking a Web service
Requirements Description:
Enter a number n arbitrarily, return the sum of the 1+2+...+n, and display it in the console
Implementation ideas:
1. Create the Service interface Sumservice.java
2. Create a client call class Client.java
3, the operation of digital accumulation and Web services
4. Run client invoke service, pass in parameters, get running result

Create a Web Service project click New Web ServiceProject:sumwebservice WebService Select the second rest click Next now user Libraries Select the xfire you just created
Configure Web. xml
  <servlet>  <display-name>XFireServlet</display-name>  <servlet-name>xfireservlet </servlet-name>  <servlet-class>org.codehaus.xfire.transport.http.xfireconfigurableservlet</ servlet-class>  <init-param>  <param-name>config</param-name>  <param-value >services.xml</param-value>  </init-param>  <load-on-startup>1</load-on-startup >  </servlet>  <servlet-mapping>  <servlet-name>xfireservlet</servlet-name >  <url-pattern>/services/*</url-pattern>  </servlet-mapping>

To create a new interface and implementation class:

New interface:

Cn.edu.hqu.service under Sumservice;

Package Cn.edu.hqu.service;public interface Sumservice {/** * Returns the accumulation of 1-n and * @param n * @return */public int getsum (int n);}
The new implementation class implements SUMSERVICECN.EDU.HQU.SERVICE.IMPL under Sumserviceimpl:
Package Cn.edu.hqu.service.impl;import Com.service.sumservice;public class Sumserviceimpl implements Sumservice { public int getsum (int n) {int sum = 0;for (int i = 1; I <= n; i++) {sum + = i;} return sum;}}

Create a new services.xml under SRC
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://xfire.codehaus.org/config/1.0" ><service ><name>SumService</name><serviceClass>cn.edu.hqu.service.SumService</serviceClass> <implementationClass>cn.edu.hqu.service.impl.SumServiceImpl</implementationClass><style> wrapped</style><use>literal</use><scope>application</scope></service></ Beans>

Deploy to tomcat boot, access localhost:8080/sumwebservice/services

Calls to Web services
Get valid WSDL address

Create a new client Web Project:client
Introduction to the service-side project Sumwebservice
Right-click Project, Properties


Introduce your own Xfire tag library: Right-click the project build Path→add Libra. →user Library Next Select Xfire Complete;

Client New Sumclinet Class
public static void Main (string[] args) throws Exception {//load xfire//Create service Services service =  new Objectservicefactory () . Create (Sumservice.class);//wsdl address String url = "Http://localhost:8080/SumWebService/services/SumService"; Xfireproxyfactory factory = new Xfireproxyfactory (Xfirefactory.newinstance (). Getxfire ()); Sumservice Sumservice = (sumservice) factory.create (service, URL),//Ensure that the server has an open System.out.println (Sumservice.getsum ( 100));}
Before running, the WebService server (Sumwebservice) must first deploy the boot;
Run Main, console output 5050;
But such two items are coupled together;
Another way to Sumclinet:

public static void Main (string[] args) throws Exception {String wsdllocation = "http://localhost:8080/SumWebService/ SERVICES/SUMSERVICE?WSDL "; Client client = new Client (new URL (wsdllocation)); object[] o =client.invoke ("Getsum", New object[]{100}); System.out.println (O[0]);}
Run the main output result;
Additional notes:Click Web. xml
The following org.codehaus.xfire.transport.http.XfireConfigurableServlet into the class:
We can see
Private final static String Config_file = "Meta-inf/xfire/services.xml";
The default path is the Meta-inf/xfire/services.xml
In the source code:
Private String Getconfigpath ()    {        if (Configpath = = NULL | | configpath.length () = = 0)        {            return config_ FILE;        }        return configpath;    }

The path will be obtained at the beginning, and if the path is empty, the default path will be used;

So we'll buy it in the Wen.xml. Specify configpath path, src write file name directly

  <init-param>  <param-name>config</param-name>  <param-value>services.xml</ Param-value>  </init-param>
This will not be an error, or you can put Services.xml in the new Meta-inf/xfire/services.xml;

Xfire Download: Http://pan.baidu.com/s/1ntx8yLF
Code: Http://pan.baidu.com/s/1vmXtG

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.