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
- Public interface Ireaderservice {
- Public Reader getreader (String name,string password);
- Public list<reader> getreaders ();
- }
There is an interface, of course, there must be an implementation class, otherwise the interface is meaningless.
Java code
- Public class Readerservice implements ireaderservice{
- Public Reader getreader (String name,string password) {
- return new Reader (Name,password);
- }
- Public list<reader> getreaders () {
- list<reader> readerlist = new arraylist<reader> ();
- Readerlist.add (new Reader ("shun1","123"));
- Readerlist.add (new Reader ("shun2","123"));
- return readerlist;
- }
- }
Also look at the Javabean,reader class:
Java code
- Public class reader{
- private Static final long serialversionuid = 1L;
- private String name;
- private String password;
- Public Reader () {}
- Public Reader (String name,string password) {
- this.name = name;
- This.password = password;
- }
- //get/set Method omitted
- Public String toString () {
- return "Name:" +name+", Password:" +password;
- }
- }
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
- <? XML version= "1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <service>
- <!--webserviceq name, which you need to specify when calling--
- <name>readerservice</name>
- <!--This is usually your company's Web site, meaning not much--
- <namespace>http://test/helloservice</namespace>
- <!--interface class --
- <serviceclass>com.xfire.servlet.ireaderservice</serviceclass>
- <!--Implementation class --
- <implementationclass>com.xfire.servlet.readerservice</implementationclass >
- </Service>
- </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
- <? XML version= "1.0" encoding="UTF-8"?>
- <Web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_ 2_5.xsd "
- xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "
- id= "webapp_id" version="3.0">
- <servlet>
- <servlet-name>xfireservlet</servlet-name>
- <servlet-class>org.codehaus.xfire.transport.http.xfireconfigurableservlet</ Servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>xfireservlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- </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
- Public class Readerclient {
- public static void Main (string[] args) {
- //This is to create a service that needs to pass in an interface class, because we must call the corresponding interface method later
- Service Srcmodel = new Objectservicefactory (). Create (Ireaderservice. Class);
- //Agent factory, this is to create the corresponding interface class later
- Xfireproxyfactory factory = new Xfireproxyfactory (Xfirefactory.newinstance (). Getxfire ());
- //webservice address, no need to add WSDL
- String Readerserviceurl = "Http://localhost:8080/xfireWebService/services/readerService";
- try {
- //Use factory to return the corresponding interface class
- Ireaderservice Readerservice = (ireaderservice) factory.create (Srcmodel,readerserviceurl);
- Reader reader = Readerservice.getreader ("Shun","123");
- SYSTEM.OUT.PRINTLN (reader);
- } catch (Malformedurlexception e) {
- E.printstacktrace ();
- }
- }
- }
In this way, we see that the output is:
Webservice-java-xfire