...
First we'll write our service interface. It will have one operation called "sayHello" which says "Hello" to whoever submits their name.
Code Block | ||||
---|---|---|---|---|
| ||||
/**
* 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 may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is 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: service
package demo.spring.service;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
String sayHi(String text);
}
// END SNIPPET: service
| ||||
Wiki Markup | ||||
{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/service/HelloWorld.java} |
Our implementation will then look like this:
Code Block | ||||
---|---|---|---|---|
| ||||
/**
* 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 may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is 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: service
package demo.spring.service;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.spring.service.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String text) {
System.out.println("sayHi called");
return "Hello " + text;
}
}
// END SNIPPET: service
| ||||
Wiki Markup | ||||
{snippet:id=service|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/service/HelloWorldImpl.java} |
The @WebService
annotation on the implementation class lets CXF know which interface to use when creating WSDL. In this case its simply our HelloWorld interface.
...
Lets create a "cxf-servlet.xml" file in our WEB-INF directory which declares an endpoint bean:
Code Block | ||||
---|---|---|---|---|
| ||||
<?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 may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is 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-4.2.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="demo.spring.service.HelloWorldImpl" address="/HelloWorld"/>
</beans>
<!-- END SNIPPET: beans --> | ||||
Wiki Markup | ||||
{snippet:id=beans|lang=java|url=cxf/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/webapp/WEB-INF/cxf-servlet.xml} |
If you want to reference a spring managed-bean, you can write like this:
Code Block | ||||
---|---|---|---|---|
| ||||
<bean id="hello" class="demo.spring.service.HelloWorldImpl" /> <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> |
The bean uses the following properties:
...
Since we're relying on the default "cxf-servlet.xml" file the default a web.xml referenced by many samples can be used.
...
- the Spring
ContextLoaderLister
. This starts Spring and explicitly loads the configuration file. We can specify where our file is via acontext-param
element.
An example:
Code Block | ||||
---|---|---|---|---|
| ||||
<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>
|
...
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:
Code Block | ||||
---|---|---|---|---|
| ||||
<?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:
Code Block | ||||
---|---|---|---|---|
| ||||
ApplicationContext context = ...; // your Spring ApplicationContext HellWorldHelloWorld client = (HelloWorld) context.getBean("helloClient"); |
...
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:
...
Code Block | ||||
---|---|---|---|---|
| ||||
<?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 may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is 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-4.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
<bean id="client" class="demo.spring.service.HelloWorld" factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="demo.spring.service.HelloWorld"/>
<property name="address" value="http://localhost:9002/services/HelloWorld"/>
</bean>
</beans>
<!-- END SNIPPET: beans -->
|
...
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!):
Code Block | ||||
---|---|---|---|---|
| ||||
ApplicationContext context = ...; // your Spring ApplicationContext
HelloWorld client = (HelloWorld) context.getBean("client");
|
client code at httphttps://svngithub.com/apache.org/reposcxf/asfblob/cxfmaster/trunk/distribution/src/main/release/samples/java_first_spring_support/src/main/java/demo/spring/client/Client.java
...
Some usage scenarios will require more extensive configuration (and this is not the case with the <jaxws:client>
syntax described above). For more information, see JAX-WS Configuration.
Advanced Steps
For more information on using Spring you may want to read the Configuration and Spring sections of the User's Guide.