Apache cxf+spring development environment to build a small test

Source: Internet
Author: User
Tags soap wsdl

Recently a project to develop WebService, and the original system used spring, so in the selection framework, I chose the cxf, so in the development of integration is more convenient. In the process of building the development environment found this article is written in more detail, so moved to their own blog, hoping to give themselves and peers to do the reference.

CXF Application Development
Here's the start of our CXF Web Services development Journey! First, there's an Eclipse-based development environment, and then we'll use this development environment to develop a simple "poll-poll" example, and we'll explain some of the basic ways in which CXF is configured in development.

Development Environment Preparation
Before development, we usually need to do some preparatory work, such as building a good development environment. In this tutorial, we'll develop in the Eclipse 3.2 environment, and of course if you're using Eclipse Europa (Eclipse 3.3), let's say you've downloaded and installed the Eclipse development environment on your computer (about how to download and install Eclipse, see Reference resources).


Create a project skeleton
Start Eclipse, create a Java project, if it is a WTP, you can create a Web project of the EE, we name Cxf_spring_survey, and set the output path of the compilation to Web-inf/classes Deployment of applications directly to the user.
The directory structure looks like this:

Figure 4: Engineering skeleton for Web Services development with CXF


For convenience, we directly copy all the. jar files under the%cxf_home%/lib directory to the Web-inf/lib directory of the Cxf_spring_survey project, or you can select only the scope of each jar package as described in the previous "CXF Installation package" section The. jar file you want. After the refresh in Eclipse, you can see the following structure:

Figure 5: Engineering that uses CXF to develop Web Services introduces the skeleton behind all. jar Files


and add these. jar to the Java Build Path in the Cxf_spring_survey project properties, such as:

Figure 6. After all the. jar files are introduced in Eclipse


In this way, the basic skeleton of the project has been created, and the next step is to write the interface and implementation code.

Interface class creation
Create a new WS.CXF package in the SRC directory of the project, creating the interface class Isurveyservice.java inside, for a simple example, we only create a method public string vote (string Username,int point ); It is important to note that we use @WebService annotations on the interface to indicate that this is an interface that will be exposed as a Web Service and expose the methods inside. The complete interface code listing is as follows:

Java code
    1. PACKAGEWS.CXF;
    2. Importjavax.jws.WebService;
    3. @WebService
    4. PublicInterfaceisurveyservice
    5. {
    6. /**
    7. * @paramusername Name
    8. * @parampoint Score
    9. * @return
    10. */
    11. Publicstringvote (Stringusername,intpoint);
    12. }


Next, we implement it according to the definition of the interface.

Concrete class implementation
For the definition of the interface, we create a corresponding implementation class and define it in the Sw.cxf.impl package, the complete code listing is as follows:

Java code
  1. Packagews.cxf.impl;
  2. Importjavax.jws.WebService;
  3. Importws.cxf.ISurveyService;
  4. @WebService
  5. Publicclasssurveyserviceimplementsisurveyservice
  6. {
  7. Privatestringexcludename="Michael";
  8. Privateintleastponit=5;
  9. Publicstringvote (Stringusername,intpoint)
  10. {
  11. stringresult="";
  12. if (Excludename.equals (username))
  13. {
  14. result="You can't vote again!" ";
  15. }
  16. Else
  17. {
  18. result="Thank you for your vote!" ";
  19. if (point<leastponit)
  20. {
  21. result+="Your voting scores are too low! ";
  22. }
  23. Else
  24. {
  25. result+="Your voting score is approved! ";
  26. }
  27. }
  28. Returnresult;
  29. }
  30. Forioc
  31. Publicstringgetexcludename ()
  32. {
  33. Returnexcludename;
  34. }
  35. Publicvoidsetexcludename (stringexcludename)
  36. {
  37. This.excludename=excludename;
  38. }
  39. Publicintgetleastponit ()
  40. {
  41. Returnleastponit;
  42. }
  43. PublicVoidsetleastponit (intleastponit)
  44. {
  45. This.leastponit=leastponit;
  46. }
  47. }



The interface definition and the concrete implementation is so simple to complete, then the related configuration work, first of all, Spring Bean configuration.


Spring Configuration
Create a beanrefserver.xml file in the SRC directory that defines the configuration of the spring Bean, CXF supports the Spring 2.0 Schema label configuration, and provides a label for the quick exposure Web Services.

First, we need to introduce the Spring and CXF Namespaces (namespace) as follows:

Java code
    1. <beansxmlns="Http://www.springframework.org/schema/beans"
    2. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:jaxws="Http://cxf.apache.org/jaxws"
    4. Xsi:schemalocation= "
    5. http://www.springframework.org/schema/beans
    6. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    7. http://cxf.apache.org/jaxws
    8. http://cxf.apache.org/schemas/jaxws.xsd ">



In this way, we can use the Spring and CXF label configuration. Next, we need to introduce the Bean definition file of the CXF we need, as follows:

Java code
    1. <!--importapachecxfbeandefinition-->
    2. <importresource="Classpath:meta-inf/cxf/cxf.xml"/>
    3. <importresource="Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
    4. <importresource="Classpath:meta-inf/cxf/cxf-servlet.xml"/>


We then define the bean that we implement specifically, which is the same definition as the Spring normal bean definition:

Java code
    1. <!--surveyservice-->
    2. <beanid= "surveyservice"class="Ws.cxf.impl.SurveyService" >
    3. <propertyname= "excludename" value= "Michael"/>
    4. <propertyname= "leastponit" value= "ten"/>
    5. </bean>


Finally, exposing the defined Bean as a Web service and configuring <jaxws:server> with the Schema tag provided by CXF, the defined configuration is more concise and convenient, as defined below:

Java code
    1. <!--exposesurveywebservice-->
    2. <jaxws:serverid="Surveywebservice"
    3. serviceclass="Ws.cxf.ISurveyService"
    4. address="/surveywebservice" >
    5. <jaxws:serviceBean>
    6. <refbean="Surveyservice"/><!--a reference to the bean to be exposed- -
    7. </jaxws:serviceBean>
    8. </jaxws:server>



In the configuration, the value of the ServiceClass is the name of our interface class, which addresses the WEB Service access address that will be exposed. For example:/surveywebservice, the address of the client consuming Web Service becomes http://host:port/WebAPPName/SurveyWebService, and the corresponding WSDL address is: http:/ /host:port/webappname/surveywebservice?wsdl.

Web app Configuration
Since our example is the need for service exposure through a Servlet container, we need to configure the corresponding Web. xml file, first of all to increase the Spring configuration file load Listener, as follows:

Java code
    1. <!--springconfiglocation-->
    2. <context-param>
    3. <param-name>contextConfigLocation</param-name>
    4. <param-value>/WEB-INF/classes/beanRefServer.xml</param-value>
    5. </context-param>
    6. <!--springcontextloaderlistener-->
    7. <listener>
    8. <listener-class>
    9. Org.springframework.web.context.ContextLoaderListener
    10. </listener-class>
    11. </listener>


Next, configure the definition of the CXF Servlet, along with its mappings, as follows:

Java code
    1. <!--apachecxfservlet-->
    2. <servlet>
    3. <servlet-name>CXFServlet</servlet-name>
    4. <display-name>CXFServlet</display-name>
    5. <servlet-class>
    6. Org.apache.cxf.transport.servlet.CXFServlet
    7. </servlet-class>
    8. <load-on-startup>1</load-on-startup>
    9. </servlet>
    10. <!--cxfservletmapping-->
    11. <servlet-mapping>
    12. <servlet-name>CXFServlet</servlet-name>
    13. <url-pattern>/*</url-pattern>
    14. </servlet-mapping>



We'll map it to/*. That way, the server's code and configuration are all done, and the next step is to deploy the application to the Web container and verify that the service is published properly.

Application Deployment


We deploy the app to Tomcat 5.5.25, where we use the link deployment method, which is simple and easy to create with the project name in the%tomcat_home%/conf/catalina/localhost/directory cxf_ Spring_survey consistent XML file: Cxf_spring_survey.xml, Content:
<?xml version= "1.0" encoding= "UTF-8"?
<context Docbase= "F:/javaproject/webservice/cxf/cxf_spring_survey"/> the contents of the

DocBase are changed according to the directory in which your actual project is located, and be careful to use/instead of \.

Start the service
start Tomcat at this point, during startup, you can see in the Startup window that an app that is deployed as a link will print some relevant information in the startup and finally show the startup success. You can see the CXF exposed service link by accessing http://localhost:8080/CXF_Spring_Survey/:

Figure 7. CXF the contents of the exposed service link


can be accessed directly by clicking into it, or by manually entering the address of the WSDL: Http://localhost:8080/CXF_Spring_Survey/SurveyWebService? WSDL, you can see the following WSDL content:

Figure 8. Surveywebservice WSDL content


So we can make sure our service is really successful, and then we can use the client to consume it.

Consumer Services
Go back to the Eclipse development platform and begin writing consumer service-related code, first defining the client Bean for the WEB service through the configuration of Spring and CXF, and creating the Beanrefclient.xml in the SRC directory Configuration file, we also need to introduce the declaration of the Spring and CXF namespaces and introduce the CXF Bean's definition file, and finally define the Client Access Service statement by CXF tag <jaxws:client> with the server-side configuration, complete definition of the content as follows:

Java code
  1. <?xmlversion= "1.0" encoding="UTF-8"?>
  2. <beansxmlns="Http://www.springframework.org/schema/beans"
  3. xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:jaxws="Http://cxf.apache.org/jaxws"
  5. Xsi:schemalocation= "
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. http://cxf.apache.org/jaxws
  9. http://cxf.apache.org/schemas/jaxws.xsd ">
  10. <!--importapachecxfbeandefinition-->
  11. <importresource="Classpath:meta-inf/cxf/cxf.xml"/>
  12. <importresource="Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
  13. <importresource="Classpath:meta-inf/cxf/cxf-servlet.xml"/>
  14. <!--surveywebserviceclient-->
  15. <jaxws:clientid="Surveyserviceclient"
  16. serviceclass="Ws.cxf.ISurveyService"
  17. address="Http://localhost:8080/CXF_Spring_Survey/SurveyWebService"/>
  18. </beans>



Definition Description: ID is a Spring-defined ID, used to obtain its identity in the program, ServiceClass is still the interface class defined for the server, address is the full Web service addresses, this is not the same as the definition of the server.
After defining the configuration file, we then write the specific code for the access, create the Ws.cxf.client package in the test directory, and then create the Surveyserviceclient.java, complete with the following code:

Java code
  1. Packagews.cxf.client;
  2. Importorg.springframework.context.ApplicationContext;
  3. Importorg.springframework.context.support.ClassPathXmlApplicationContext;
  4. Importws.cxf.ISurveyService;
  5. Publicclasssurveyserviceclient
  6. {
  7. PublicstaticVoidmain (String[]args)
  8. {
  9. Load the client's configuration definition
  10. applicationcontextcontext=New
  11. Classpathxmlapplicationcontext ("Beanrefclient.xml");
  12. Gets the defined Webservicebean
  13. Isurveyservicesurveyservice=
  14. (Isurveyservice) Context.getbean ("surveyserviceclient");
  15. 1. Define the variables and contents of the poll to send to the service
  16. Stringusername="Test";
  17. Intpoint=;
  18. Calling methods for service consumption
  19. Stringresult=surveyservice.vote (Username,point);
  20. System.out.println ("Result:" +result);
  21. 2, the transmission of a different poll content
  22. Username="Michael";
  23. Point=;
  24. Call the method again to consume the service and get a different result
  25. Result=surveyservice.vote (Username,point);
  26. System.out.println ("Result:" +result);
  27. 3. Third Pass and call
  28. Username="Jordan";
  29. point=9;
  30. Result=surveyservice.vote (Username,point);
  31. System.out.println ("Result:" +result);
  32. }
  33. }



Directly run the above client consumer program, a total of three calls to the Web Service, and the results are as follows:
Result: Thank you for your vote! Your voting score is approved!
Result: You cannot vote repeatedly!
Result: Thank you for your vote! Your voting scores are too low!

The service is then properly called and the results are returned correctly, and the complete code and configuration file can be downloaded from the download link in this tutorial.


Original address: http://www.ibm.com/developerworks/cn/education/java/j-cxf/section5.html

Apache cxf+spring development environment to build a small test

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.