Writing a service with SpringPage edited by Glen Mazza
Comment:
updated page per updates made to underlying java-first sample.
Changes (9)
Full ContentThis example will lead you through creating your first service with Spring. You'll learn how to:
This example corresponds to the java_first_spring_support example in the CXF distribution. Setting up your buildOpen up your favorite IDE and create a new project. The first thing we need to do is add the necessary CXF dependencies to the project. You can find these dependencies in the CXF distribution in the lib directory. commons-logging-1.1.1.jar geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar) geronimo-annotation_1.0_spec-1.1.jar (JSR 250) geronimo-javamail_1.4_spec-1.7.1.jar (or Sun's JavaMail jar) geronimo-servlet_3.0_spec-1.0.jar (or Sun's Servlet jar) geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181) jaxb-api-2.1.jar jaxb-impl-2.1.13.jar jaxws-api-2.1.jar neethi-3.0.0.jar saaj-api-1.3.jar saaj-impl-1.3.jar stax-api-1.0.1.jar stax2-api-3.1.1.jar wsdl4j-1.6.2.jar woodstox-core-asl-4.0.8.jar xmlschema-core-2.0.jar xml-resolver-1.2.jar
To provide a bean name instead of a classname as an implementor, simply supply the bean-name prepended with "#", e.g. implementor="#myBean". You can also do more sophisticated things with the <jaxws:endpoint> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the service. For more on this see JAX-WS Configuration. Setting up the ServletSince we're relying on the default "cxf-servlet.xml" file the default web.xml referenced by many samples can be used. Alternatively, for arbitrarily named configuration files such as beans.xml, application-context.xml, etc. we can add the following elements:
An example:
<web-app ...>
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
It is important to note that the address that you chose for your endpoint bean must be one your servlet listens on. For instance, if my Servlet was register for "/some-services/*" but my address was "/more-services/HelloWorld", there is no way CXF could receive a request. Create a Client (Easy Way)Just like the <jaxws:endpoint> used on the server side, there is a <jaxws:client> that can be used on the client side. You'll give it a bean name, the service interface, and the service URL, and it will create a bean with the specified name, implementing the service interface, and invoking the remote SOAP service under the covers: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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="helloClient" serviceClass="demo.spring.HelloWorld" address="http://localhost:9002/HelloWorld" /> </beans> You can now inject that "helloClient" bean into any other Spring bean, or look it up from the Spring application context manually with code like this: ApplicationContext context = ...; // your Spring ApplicationContext HellWorld client = (HelloWorld) context.getBean("helloClient"); You can also do more sophisticated things with the <jaxws:client> element like add nested tags to attach JAX-WS Handlers or CXF Interceptors to the client. For more on this see JAX-WS Configuration. Create a Client (More Manual Way)CXF includes a JaxWsProxyFactory bean which create a client for you from your service interface. You simply need to tell it what your service class is (the HelloWorld interface in this case) and the URL of your service. You can then create a client bean via the JaxWsProxyFactory bean by calling it's create() method. Here's an example: Error formatting macro: snippet: java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
If you were going to access your client you could now simply pull it out of the Spring context (or better yet, inject it into your application using Spring!): ApplicationContext context = ...; // your Spring ApplicationContext HelloWorld client = (HelloWorld) context.getBean("client");
Advanced StepsFor more information on using Spring you may want to read the Configuration and Spring sections of the User's Guide.
Change Notification Preferences
View Online
|
View Changes
|
Add Comment
|
- [CONF] Apache CXF Documentation > Writing a service with Spr... confluence
