Use cxf to create WebService

Source: Internet
Author: User

1. Create a web project.

2,
Introduce spring and cxf packages.

3,
Create a service Terminal interface (sayhello)

Package zxf. cxf. Service;

Import javax. JWS. webmethod;

Import javax. JWS. WebService;

@ WebService

Public interface sayhello {

@ Webmethod

Public String say (string name );

}

4,
Create a service implementation class (sayhello)

Package zxf. cxf. Service;

 

Public class sayhelloimpl implements sayhello {

@ Override

Public String say (string name ){

System. Out. println ("Hello! ");

Return "hello" + name;

}

}

5,
Create a configuration file (bean. XML) for the server bean)

<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-3.0.xsd

Http://cxf.apache.org/jaxws

Http://cxf.apache.org/schemas/jaxws.xsd>

<! -- The following configuration is used to create the bean object required by cxf -->

<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 terminal -->

<Jaxws: endpoint id = "helloservice" implementor = "zxf. cxf. Service. sayhelloimpl"

Address = "/sayhello"/>

</Beans>

6,
Configure the Web file (with spring listener and cxf servlet)

<? 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_2_5.xsd "id =" webapp_id "version =" 2.5 ">

<Display-Name> cxf_service </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>

<! -- Specify the location of the spring configuration file using the preceding parameters -->

<Context-param>

<Param-Name> contextconfiglocation </param-Name>

<Param-value> classpath: Beans. xml </param-value>

</Context-param>

<! -- Configure the spring context loader listener to ensure that the spring container Initialization is completed when the web server is started.
-->

<Listener>

<Listener-class> org. springframework. Web. Context. contextloaderlistener </listener-class>

</Listener>

<! -- Configure the servlet of the cxf engine -->

<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>/* </url-pattern>

</Servlet-mapping>

</Web-app>

7,
Deploy the Web service to Tomcat. View the service documentation.

8,
View the WebService Description document http: // localhost: 8080/cxf_service/helloservice? WSDL

9,
Create an exclusive program for WebService.

(1)
Introduce the class library. Cxf + spring

(2)
Create the spring configuration file clent. xml

<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>

<Jaxws: Client ID = "sayhelloserviceclient"

Serviceclass = "zxf. cxf. Service. sayhello"

Address = "http: // localhost: 8080/cxf_service/helloservice"/>

</Beans>

(3) copy the sayhello interface.

(4) Create an app Client

Package zxf. cxf. Service;

Import org. springframework. Context. applicationcontext;

Import org. springframework. Context. Support. classpathxmlapplicationcontext;

Public class app {

Public static void main (string [] ARGs ){

System. Out. println (system. getproperty ("Java. endorsed. dirs "));

Applicationcontext AC = new classpathxmlapplicationcontext ("client. xml ");

Sayhello S = (sayhello) AC. getbean ("sayhelloserviceclient ");

System. Out. println (S. Say ("dfdsf "));

}

}

(5) run the app if the following error occurs:

Java. Lang. linkageerror: jaxb 2.0 API is being loaded from the bootstrap classloader, but this Ri *** needs 2.1 API.

Solution:
1. If it is an application project, print it out in the program

System. Out. println (system. getproperty ("Java. endorsed. dirs "));
Locate the appropriate location, put the jaxb-api-2.1.jar and jaxb-impl-2.1.12.jar under the corresponding directory. No endorsed directory. Create one by yourself.
2. If it is a Java Web project, print it in a project that runs like servlet:

System. Out. println (system. getproperty ("Java. endorsed. dirs "));
Locate the appropriate location, put the jaxb-api-2.1.jar and jaxb-impl-2.1.12.jar under the corresponding directory. No endorsed directory. Create one by yourself.
In my web project, the printed path is: e: \ Java \ apache-Tomcat-7.0.19 \ endorsed

The jar files placed in the endorsed directory can overwrite the system API. It can be far-fetched to put the modified API into the virtual machine, and replace it with the startup API specified by the VM.

 

 

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.