WebService Summary (1) -- use CXF to publish and call webService (do not use Spring), cxfwebservice

Source: Internet
Author: User

WebService Summary (1) -- use CXF to publish and call webService (do not use Spring), cxfwebservice

CXF and Axis2 are two popular webService frameworks. Next I will write a few blogs to briefly introduce how to use these two frameworks. First, we will briefly introduce the use of CXF.


There are multiple methods for publishing webService in CXF. Here I will introduce three methods:

1. CXF Automatically releases webService without Spring

2. If Spring is not used, CXF manually releases webService

3. Use Spring + CXF to publish webService


This blog introduces the first method as an example-without Spring, CXF Automatically releases webService.


Server:

The directory structure is as follows:


IHelloWorldServer code:

<span style="font-family:Microsoft YaHei;font-size:18px;">package com.test.server;public interface IHelloWorldServer {public String sayHello(String username); }</span>

HelloWorldServerImp code:
<span style="font-family:Microsoft YaHei;font-size:18px;">package com.test.server;public class HelloWorldServerImp implements IHelloWorldServer {@Overridepublic String sayHello(String username) {return username+" : HelloWorld";}}</span>
These two are the simplest java classes.


WebServlet code:

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> package com. test. server; import javax. servlet. servletConfig; import org. apache. cxf. bus; import org. apache. cxf. busFactory; import org. apache. cxf. frontend. serverFactoryBean; import org. apache. cxf. transport. servlet. CXFNonSpringServlet; public class WebServlet extends CXFNonSpringServlet {// private static final String SERVICE_SUFFIX = ""; private static final long serialVersionUID = 1L; @ Override protected void loadBus (ServletConfig servletConfig) {super. loadBus (servletConfig); Bus bus = getBus (); BusFactory. setDefaultBus (bus); HelloWorldServerImp helloWorld = new HelloWorldServerImp (); // implementation class ServerFactoryBean serverFactoryBean = new ServerFactoryBean (); // server factory serverFactoryBean. setServiceClass (IHelloWorldServer. class); // Interface class serverFactoryBean. setAddress ("/helloWorld"); // Service Request Path serverFactoryBean. setServiceBean (helloWorld); serverFactoryBean. create () ;}</span>
This Servlet is used to publish webService. After the program is deployed, the container automatically calls the following method.


Web. xml file

<span style="font-family:Microsoft YaHei;font-size:18px;"><?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"><display-name>cxf_demo</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><display-name>cxf_demo</display-name><servlet><servlet-name>CXFServlet</servlet-name><servlet-class>com.test.server.WebServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>CXFServlet</servlet-name><url-pattern>/ws/*</url-pattern></servlet-mapping></web-app></span>

After the above project is published to Tomcat, it will automatically publish webService. Enter the URL: http: // localhost: 8080/cxf_demo_noSpring_1/ws to view the published webService. Enter the URL: http: // localhost: 8080/cxf_demo_noSpring_1/ws/helloWorld? Wsdl: view the wsdl file. It is displayed normally, indicating that the webService has been released successfully.


Client:

HelloWorldClient code:

<Span style = "font-family: Microsoft YaHei; font-size: 18px;"> package com. test. client; import org. apache. cxf. jaxws. endpoint. dynamic. jaxWsDynamicClientFactory; public class HelloWorldClient {public static void main (String [] args) {JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory. newInstance (); org. apache. cxf. endpoint. client client = dcf. createClient ("http: /localhost: 8080/cxf_demo_noSpring_1/ws/he Looworld? Wsdl "); Object [] objects; try {objects = client. invoke ("sayHello", "haitao"); // output the call result System. out. println (objects [0]. toString ();} catch (Exception e) {e. printStackTrace () ;}}</span>

The result is as follows:



So far, the first method has been introduced, and I will continue to introduce other methods in the next blog.




How to configure spring when the client of the CXF Framework calls the webservice released by AXIS

Through the wsdl2java tool of CXF, the java code is generated and called directly.

CXF integrates spring to implement webservice. This is included in the spring configuration file: (as follows) What are the configuration files?

Is the configuration file in the cxf-*. jar package that is reflected in the project, such as my cxf-2.4.2.jar/META-INF/cxf/cxf-servlet.xml and so on.
 

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.