Webservice-java-xfire

Source: Internet
Author: User
Tags wsdl

Recently, the company recently needed to use the interface previously provided a unified standard to achieve, considering webservice this is the standard, so I took the time to learn about WebService, but also some Java WebService framework for some small examples of learning.

Java call WebService, when you first start to contact you will feel it is a nightmare, especially without a unified standard implementation, compared to the. NET of those few steps can be completed WebService implementation, we look at the implementation of Java is really sad ah. But it is sad, we still have to finish. Java also has a better implementation, such as XFIRE,JERSEY,CXF. Some people will say axis2, that thing, look at the sad, it is not included in the better inside, than the poor inside it has a seat. How bad, here first not to say, we slowly look at the implementation of these several frameworks.

Today, we'll take a look at the implementation of Xfire, and we'll take the next few days to learn about the other framework.

1) First of all, of course, the package, the ordinary people know. Http://xfire.codehaus.org/Download can go down here, can under all also can under distribution. But the proposal is still under all, lest a bunch of strange problems make you have no confidence.

What if we get the bag? Put it in the project. Seemingly nonsense, but a lot of people just don't know what to do with it.

Build a new project, compare mine is Xfirewebservice, here is of course build Web project.



I am here to put all of its bags here, after all, we write an example, there is no need to pick, casually, if you want to see the abnormal information of friends can not put all in, slowly join, later encountered errors are also excluded, but we do not do so here, After all, the general lack of what kind of the exception is no ugly, we can exclude themselves.

2) Let us first understand the differences between Xfire and other WebService frameworks, the biggest difference is that it requires an interface, and if you need to use Xfire to invoke the corresponding WebService must know the definition of the interface, it feels a bit limited here. But in addition to this, Xfire calls WebService, which is quite handy, just like calling a local method. We look directly at the example:

The first is the most important interface:

Java code
    1. Public interface Ireaderservice {
    2. Public Reader getreader (String name,string password);
    3. Public list<reader> getreaders ();
    4. }

There is an interface, of course, there must be an implementation class, otherwise the interface is meaningless.

Java code
  1. Public class Readerservice implements ireaderservice{
  2. Public Reader getreader (String name,string password) {
  3. return new Reader (Name,password);
  4. }
  5. Public list<reader> getreaders () {
  6. list<reader> readerlist = new arraylist<reader> ();
  7. Readerlist.add (new Reader ("shun1","123"));
  8. Readerlist.add (new Reader ("shun2","123"));
  9. return readerlist;
  10. }
  11. }

Also look at the Javabean,reader class:

Java code
  1. Public class reader{
  2. private Static final long serialversionuid = 1L;
  3. private String name;
  4. private String password;
  5. Public Reader () {}
  6. Public Reader (String name,string password) {
  7. this.name = name;
  8. This.password = password;
  9. }
  10. //get/set Method omitted
  11. Public String toString () {
  12. return "Name:" +name+", Password:" +password;
  13. }
  14. }

Note that the reader class here implements the Serializable interface, for what? Here, first we need to understand the principle of webservice, for Java, if we need to upload objects on the internet, a lot of people will certainly think of serialization, right, this is the serialization, because we need to use reader as a parameter to pass. This in the previous version is mandatory implementation, or will be error, but now the latest version (in fact, the latest is also 07 years, because Xfire has stopped development, was the Apache merger into the CXF project, which we later said) no longer need, as to how it was done, We are not here for the moment, because it has been merged into the CXF, if we want to study deeply, we should learn cxf better.

3) When we finish the above interface and JavaBean writing, many people will ask, I see a lot of WebService will have WSDL file, then how did you come here? Before we talk about this, let's discuss what WSDL is. Perhaps many of the interfaces provided by the company are still just an HTTP address, returning the format of XML, and so is ours. There is a benefit, there is a disadvantage. The advantage is that we are less difficult to develop, and the downside is that we need to provide users with a bunch of documentation, each return of the XML tag is what meaning, this is nothing, but it is more annoying. And webservice, the downside is that we develop a little bit more, and the advantage is that we do not have to write so many documents, because there is a unified description, called WSDL, this is the WebService document, is unified, no matter what language is the same, So there is no question of who can not understand.

And here, when we deploy the Xfire, it can help us generate the WSDL file.

The problem is how to deploy it, which is actually simple. We create a new folder in the SRC directory meta-inf, and then build it a word folder Xfire, inside the file services.xml. The following structure follows:


Someone would ask why it was built into the SRC directory, but it's not a rule to build here, but because we need the development tools to help us deploy the files ourselves, we put it here and eclipse can help us deploy ourselves to Tomcat or other containers. Note that the folder hierarchy in which this file resides is fixed and cannot be modified.

Let's look directly at Servics.xml:

XML code
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://xfire.codehaus.org/config/1.0">
  3. <service>
  4. <!--webserviceq name, which you need to specify when calling--
  5. <name>readerservice</name>
  6. <!--This is usually your company's Web site, meaning not much--
  7. <namespace>http://test/helloservice</namespace>
  8. <!--interface class --
  9. <serviceclass>com.xfire.servlet.ireaderservice</serviceclass>
  10. <!--Implementation class --
  11. <implementationclass>com.xfire.servlet.readerservice</implementationclass >
  12. </Service>
  13. </Beans>


It's okay to look at the notes in general.

4) A lot of people think this is OK, no, not yet, you have specified this, then how others access it. How to forward the corresponding request to xfire there, let it handle it. We need to modify the Web. Xml again.
The following changes are followed:

XML code
  1. <? XML version= "1.0" encoding="UTF-8"?>
  2. <Web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_ 2_5.xsd "
  4. xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "  
  5. id= "webapp_id" version="3.0">
  6. <servlet>
  7. <servlet-name>xfireservlet</servlet-name>
  8. <servlet-class>org.codehaus.xfire.transport.http.xfireconfigurableservlet</ Servlet-class>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>xfireservlet</servlet-name>
  12. <url-pattern>/services/*</url-pattern>
  13. </servlet-mapping>
  14. </Web-app>

In fact, it adds a servlet and a corresponding mapping. Next, we enter directly on the browser:
http://localhost:8080/xfireWebService/services/readerService?wsdl

We can see:




The WSDL is shown here, it shows the method we define, the type of return, this file we don't analyze here, http://webservices.group.iteye.com/group/topic/11467 this friend writes it very clearly, Friends who want to know can come here and see.

5) After the completion of the four steps above, we have completed the deployment of WebService. Others can call the appropriate webservice to access our methods. Below we will use Xfire provided by the client to visit the webservice we have just released:

Java code
  1. Public class Readerclient {
  2. public static void Main (string[] args) {
  3. //This is to create a service that needs to pass in an interface class, because we must call the corresponding interface method later
  4. Service Srcmodel = new Objectservicefactory (). Create (Ireaderservice.   Class);
  5. //Agent factory, this is to create the corresponding interface class later
  6. Xfireproxyfactory factory = new Xfireproxyfactory (Xfirefactory.newinstance (). Getxfire ());
  7. //webservice address, no need to add WSDL
  8. String Readerserviceurl = "Http://localhost:8080/xfireWebService/services/readerService";
  9. try {
  10. //Use factory to return the corresponding interface class
  11. Ireaderservice Readerservice = (ireaderservice) factory.create (Srcmodel,readerserviceurl);
  12. Reader reader = Readerservice.getreader ("Shun","123");
  13. SYSTEM.OUT.PRINTLN (reader);
  14. } catch (Malformedurlexception e) {
  15. E.printstacktrace ();
  16. }
  17. }
  18. }

In this way, we see that the output is:


Webservice-java-xfire

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.