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 downloads and Eclipse installation maven plugins.

Second, step

1. Create a new Web project, using Maven management, as follows:


Project name is test, after completion, the structure of projects such as:


Src/main/java ready to put 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's just a webservice experimental procedure, it's very simple, only one service method: SayHello (), using @WebService annotations to declare that this is a 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!";    }}

The project structure after the completion of the Java code is as follows:


3. Add SPRING-CXF Configuration

Create a new XML definition under the project Src/main/webapp/web-inf directory: Cxf-servlet.xml as follows:

<?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/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 (corresponding absolute directory 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, add apache-cxf dependencies, such as:


4. Web application Configuration

Modify the Web. xml file under the Src/main/webapp/web-inf directory

<?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 works in steps 3 and 4:


5. Compiling the package

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

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

6. Deploy the Test.war generated by step 5 to the Tomcat server

7. Access Test:

Enter: http://localhost:8080/test/on the browser, the following screen appears to succeed:


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 in the Src\main\resources directory:

<?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 in this configuration file, the Address you need to write the absolute path to the Publishing service.

2) Writing the client Java 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    }}

Note that the code helloworldclient = (HelloWorld) Context.getbean ("Client"); Client needs to match the bean ID in the "Client-beans.xml" to find the service.

The current project structure is as follows:


3) connection test

Press helloworldclient directly in Eclipse to run the Java application:

The output of the Hello world! That is the output of the HelloWorld method SayHello () that we released! This indicates that the connection from the service publishing 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.