Use wsdl2java of cxf to establish web service.

Source: Internet
Author: User

At the beginning, xfire was exposed. Because xfire has not been maintained for several years, its compatibility is not very good, and some junk things are automatically generated in the WSDL, in particular, if the field in the class uses the generic xfire word, the WSDL is very large, and the client generates a lot of code according to the WSDL. if you have defined the WSDL, you only need to generate a client for the user. However, due to the incompatibility of xfire, it is often necessary to copy the WSDL of xfire web service after running to the user.

So today, I decided to abandon xfire and use CFX instead. Since CFX is an open-source project of Apache, it is no different in terms of compatibility. First, define a WSDL document, so there is no need to consider Element Types and constraints like xfire. The same wsdldocument uses xfireto generate the service end, and then uses other tools such as wsdl.exe to generate the client. Without proper constraints and design, the client cannot directly call the server, one feature of xfire today is that the XSD namespace will be changed to the class package path in Java, such as in the WSDL document:

<XSD: schema targetnamespace = "http://services.sxit.com/cueapinterfaceforsi/">

After the Web service is run, the WSDL is:

<XSD: schema targetnamespace = "http://cueapinterfaceforsi.services.sxit.com"/>.

In this way, you cannot directly use the original WSDL file to generate a client to interact with the web service. Of course, some people can use aegis. XML for ing, but I have never used it. I don't know if it works. However, in CFX, you do not need to consider these issues. You can define the WSDL document directly and use Clients generated by other tools to access the Web Service normally.

After defining a WSDL document, you can use the wsdl2server tool of CFX to generate class interfaces and interface implementation classes, some programmers prefer to directly write interfaces, classes and related data classes when doing Web Services, and then create Web servers. In fact, this is a little troublesome. It takes a long time to write code. Sometimes you need to pay attention to the namespace and element name. If you have written the WSDL in advance, you can directly use the tool to generate the server, it doesn't save a lot of code. If you have nothing to say, go back to the topic.

Download the CFX package from the Apache website, decompress it to a certain path, and set the environment variable:

Cxf_home = E:/Java/apache-cxf-2.1.3, and add % cxf_home %/bin in path. Then, you can enter:

Wsdl2java-impl-server cueapinterfaceforsi. WSDL is used to generate interface classes, implementation classes, and other data classes. -The impl parameter requires the generation of implementation classes. If this parameter is not provided, only interfaces and other element classes are generated.

Then build the CFX environment and create a new Web server project in myeclipse. type Selection JAX-WS. after the project is successfully created, copy all jar files under the lib directory in the CFX folder to the lib directory of the project. Copy the Java code generated using wsdl2java to the src directory of the project. then, you need to modify the wsdllocation = "file: cueapinterfaceforsi. delete the WSDL file. Otherwise, the system will prompt that the WSDL file cannot be found. create a new bean in the Web-INF directory. XML file with the following content:
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: jaxws = "http://cxf.apache.org/jaxws"
Xsi: schemalocation ="
Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
Http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd ">

<Import resource = "classpath: META-INF/cxf. xml"/>
<Import resource = "classpath: META-INF/cxf/cxf-extension-soap.xml"/>
<Import resource = "classpath: META-INF/cxf/cxf-servlet.xml"/>

<Jaxws: endpoint
Id = "cueapinterfaceforsi"
Implementor = "com. sxit. Services. cueapinterfaceforsi. cueapinterfaceforsiimpl"
Address = "/cueapinterfaceforsi"/>

</Beans>

Then configure web. xml:

<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>
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/beans. xml </param-value>
</Context-param>

<Listener>
<Listener-class>
Org. springframework. Web. Context. contextloaderlistener
</Listener-class>
</Listener>

<Servlet>
<Servlet-Name> cxfservlet </servlet-Name>
<Servlet-class>
Org. Apache. cxf. Transport. servlet. cxfservlet
</Servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet>

<Servlet-mapping>
<Servlet-Name> cxfservlet </servlet-Name>
<URL-pattern>/services/* </url-pattern>
</Servlet-mapping>
</Web-app>

Run Tomcat and enter http: // 127.0.0.1: 8080/bnetinterface/services/cueapinterfaceforsi in the URL? The content of the WSDL is the same as that of the source WSDL document. You don't have to worry about inconsistency in xfire, and it's not as complicated and huge as xfire.

Use the. net wsdl tool to generate a client based on the source WSDL document, create a test class, create a test data, and then directly call the server. The transmitted data is the same as the returned data, So you no longer have to worry about the null parameter value.

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.