ECLIPSE+MAVEN+SPRING+CXF Building WebService Services

Source: Internet
Author: User

First, software preparation

Eclipse 4.2.1

Maven 2.2.1

Spring 3.2.6

CXF 3.0.2

Please refer to other articles for software download and Eclipse install maven plugin.

Second, step

1. Create a new webproject and use Maven management. For example, the following:


Project is named Test and is finished. The project structure is for example:


Src/main/java ready to put the Java program.

Src/main/resources ready to put all kinds of resource files.

2. Add code

1) Define the service interface

Package com.test; Import Javax.jws.WebService; @WebServicepublic interface HelloWorld {public    String SayHello ();}


Because it is only a WebService experiment procedure, so easy. There is only one service method: SayHello (), using @WebService annotations to declare that this is an WebService interface.

2) Implementation of the service class

Package com.test; Import Javax.jws.WebService; @WebServicepublic class Helloworldimpl implements helloworld{public       String SayHello () {        return "Hello world!";    }}

After the Java code has been added to the project structure such as the following:


3. Join the SPRING-CXF configuration

Create a new XML definition under the Project Src/main/webapp/web-inf folder: Cxf-servlet.xml such as the following:

<?

XML version= "1.0" encoding= "UTF-8"?

><!--Licensed to the Apache software Foundation (ASF) under one or more contributor license agreement S. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless requ Ired by applicable/agreed to in writing, software distributed under the License are distributed on an ' As is ' BASIS, without warranties or CONDITIONS of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the license.-->< !--START Snippet:beans--><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/ Cxf.xml "/> <import resource=" Classpath:meta-inf/cxf/cxf-servlet.xml "/> <jaxws:endpoint id=" HelloWorld " Implementor= "Com.test.HelloWorldImpl" address= "/helloworld"/></beans><!--END Snippet:beans-

The definition file takes advantage of the functions of spring and CXF, publishes an ID of HelloWorld, implements Class Com.test.HelloWorldImpl, and publishes a relative path of/helloworld (the corresponding absolute folder is: Http://host: Port/{webappname}/helloworld) of the webservice.

Because we need to use CXF to do webservice. Right-click on the Pom.xml in the project. Join APACHE-CXF dependency. For example, with:


4. Web application Configuration

Change the Web. xml file under the Src/main/webapp/web-inf folder

<?xml version= "1.0" encoding= "UTF-8"?><!--Licensed to the Apache software Foundation (ASF) under one or more C Ontributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless required by applicable law O  R agreed to writing, software distributed under the License are distributed on a "as is" BASIS, without warranties OR CONDITIONS of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><web-app xmln s= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" version= "2.5" xsi:     Schemalocation= "Http://java.sun.com/xml/ns/javaee      Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "> <display-name>cxf</display-name> <servle t> <description>apache CXF endpoint</description> <display-name>cxf</display-name&gt        ; <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.cxfservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping&        Gt    <servlet-name>cxf</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <session-config> <session-timeout>60</session-timeout> </session-config></web-app& Gt

The file is actually a mapping relationship that defines the CXF servlet that handles webservice.

Complete the following steps 3 and 4 for the project folder:


5. Compiling the package

Use Maven (PACKAGE-X) compilation to package into Test.war

(on Eclipse, right-click Project name, Run as, Maven build)

6. Deploy the Test.war generated by step 5 to Tomcatserver

7. Visit the test:

On the browser, type: http://localhost:8080/test/. Appears for example the following screen was successful:


Click the WSDL Link:

8. Writing WebService client-side code

1) First define the client Bean for the Web Service through the configuration of Spring and CXF, and create the Client-beans.xml configuration file under the Src\main\resources folder:

<?xml version= "1.0" encoding= "UTF-8"?><!--Licensed to the Apache software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); You are not a use of this file except in compliance with the License. Obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 unless requ        Ired by applicable/agreed to in writing, software distributed under the License are distributed on an ' As is ' BASIS, without warranties or CONDITIONS of any KIND, either express or implied. See the License for the specific language governing permissions and limitations under the license.-->< !--START Snippet:beans--><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/schema/jaxws.xsd "> <bean id=" Client "cl ass= "Com.test.HelloWorld" factory-bean= "clientfactory" factory-method= "create"/> <bean id= "Clientfactory" class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <property name= "serviceclass" value= " Com.test.HelloWorld "/> <property name=" Address "value=" Http://localhost:8080/test/HelloWorld "/> </be an></beans><!--END Snippet:beans--

It is important to note that. The address in this configuration file needs to be written as an absolute path to the advertised service.

2) Write Clientjava code: Helloworldclient.java

Package com.test; Import Org.springframework.context.support.ClassPathXmlApplicationContext; Public final class Helloworldclient {     private helloworldclient () {    } public     static void Main (String args[]) t Hrows Exception {        //START snippet:client        classpathxmlapplicationcontext Context            = new Classpathxmlapplicationcontext (new string[] {"Client-beans.xml"});         HelloWorld client = (HelloWorld) Context.getbean ("Client");         String response = Client.sayhello ();        System.out.println ("Response:" + Response);        System.exit (0);        END snippet:client    }}

Attention. Code in Helloworldclient = (HelloWorld) Context.getbean ("Client"); Client needs to match the bean ID in "client-beans.xml" to find this service.

Today's project structure is as follows:


3) connection test

In Eclipse, press helloworldclient directly to execute the run as, Java application:

The output of the Hello world! That is the output of the HelloWorld method SayHello () that we published! This indicates that the connection from the service advertisement to the client was successful.

ECLIPSE+MAVEN+SPRING+CXF Building WebService Services

Related Article

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.