ApacheCXF
Apache CXF is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.
Sample project https://github.com/vborrego/cxf-test
Sample SOAP and REST Web Service
Structure:
. |-- pom.xml |-- src | `-- main | |-- java | | `-- com | | `-- test | | |-- Calculator.java | | |-- ICalculator.java | | `-- TestService.java | `-- webapp | `-- WEB-INF | |-- applicationContext.xml | `-- web.xml `-- target
pom.xml
1 <project
2 xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6 <groupId>com.test</groupId>
7 <artifactId>cxfSpringTest</artifactId>
8 <version>0.1</version>
9 <packaging>war</packaging>
10 <dependencies>
11 <dependency>
12 <groupId>org.apache.cxf</groupId>
13 <artifactId>cxf-rt-core</artifactId>
14 <version>2.4.1</version>
15 </dependency>
16 <dependency>
17 <groupId>org.apache.cxf</groupId>
18 <artifactId>cxf-bundle-jaxrs</artifactId>
19 <version>2.4.1</version>
20 </dependency>
21 <dependency>
22 <groupId>org.apache.cxf</groupId>
23 <artifactId>cxf-rt-frontend-jaxws</artifactId>
24 <version>2.4.1</version>
25 </dependency>
26 </dependencies>
27 </project>
src/main/java/com/test/Calculator.java
1 package com.test;
2
3 public class Calculator implements ICalculator
4 {
5 public Calculator() {
6 System.out.println("Calculator created ");
7 }
8
9 public long add(long num1, long num2) {
10 return (num1 + num2);
11 }
12
13 public long subtract( long num1, long num2 ){
14 return num1 - num2;
15 }
16 }
src/main/java/com/test/ICalculator.java
src/main/webapp/WEB-INF/applicationContext.xml
1 <beans xmlns="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 xmlns:jaxrs="http://cxf.apache.org/jaxrs"
5 xsi:schemaLocation=" http://www.springframework.org/schema/beans
6 http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
8 http://cxf.apache.org/jaxrs
9 http://cxf.apache.org/schemas/jaxrs.xsd
10 ">
11 <import resource="classpath:META-INF/cxf/cxf.xml"/>
12 <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
13 <!-- jax-ws -->
14 <bean id="calculatorService" class="com.test.Calculator" scope="singleton" />
15 <jaxws:endpoint implementor="#calculatorService" address="/calculator" />
16 <!-- jax-rs -->
17 <jaxrs:server id="testService" address="/">
18 <jaxrs:serviceBeans>
19 <ref bean="testService" />
20 </jaxrs:serviceBeans>
21 </jaxrs:server>
22 <bean id="testService" class="com.test.TestService" />
23 </beans>
nano src/main/webapp/WEB-INF/web.xml
1 <web-app>
2 <!-- <context-param>
3 <param-name>contextConfigLocation</param-name>
4 <param-value>/WEB-INF/applicationContext.xml</param-value>
5 </context-param> default location for spring context -->
6 <servlet>
7 <servlet-name>CXFServlet</servlet-name>
8 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
9 </servlet>
10 <servlet-mapping>
11 <servlet-name>CXFServlet</servlet-name>
12 <url-pattern>/*</url-pattern>
13 </servlet-mapping>
14 <listener>
15 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16 </listener>
17 <servlet>
18 <servlet-name>Spring</servlet-name>
19 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
20 <!-- <init-param>
21 <param-name>contextConfigLocation</param-name>
22 <param-value>/WEB-INF/applicationContext.xml</param-value>
23 </init-param> -->
24 <load-on-startup>0</load-on-startup>
25 </servlet>
26 </web-app>
src/main/java/com/test/TestService.java
1 package com.test;
2 import javax.ws.rs.GET;
3 import javax.ws.rs.Path;
4 import javax.ws.rs.PathParam;
5 import javax.ws.rs.core.Response;
6
7 @Path("/testSvc")
8 public class TestService {
9
10 @GET
11 @Path("/{param}")
12 public Response getMsg(@PathParam("param") String msg) {
13 String out = String.format("testSvc returns %s", msg);
14 return Response.status(200).entity(out).build();
15 }
16
17 }
1 mvn clean compile package # builds cxfSpringTest-0.1.war
2 # Test links:
3 # http://localhost:8080/cxfSpringTest-0.1/services
4 # http://localhost:8080/cxfSpringTest-0.1/testSvc/1001
5 # Generate client:
6 wsimport -d src/main/java -keep -p com.test.client http://localhost:8081/cxfSpringTest-0.1/calculator?wsdl
com/test/client/Client.java
1 /*
2 cd /tmp/cxfSpringTest/src/main/java
3 javac com/test/client/*.java
4 java -cp . com.test.client.Client
5 AAAA
6 5
7 */
8 package com.test.client;
9
10 public class Client{
11 public static void main(String args[] ){
12 System.out.println("AAAA");
13 CalculatorService cs = new CalculatorService();
14 ICalculator ic = cs.getCalculatorPort();
15 long res = ic.add(2,3);
16 System.out.println(res);
17 }
18 }
Get project from GitHub
1 git clone https://github.com/vborrego/cxf-test
2 cd cxf-test
3 mvn clean install
4 # http://localhost:8081/cxf-test-0.1/calculator?wsdl
5 # http://localhost:8081/cxf-test-0.1/services
6 # http://localhost:8081/cxf-test-0.1/testSvc/1001
7 # http://localhost:8081/cxf-test-0.1/?_wadl&_type=xml # save as app.wadl
8
Generate client
JAX-WS Response.status
Commonly used status codes defined by HTTP
http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.Status.html#BAD_REQUEST
http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.Status.html#OK
http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.Status.html#CONFLICT
http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.StatusType.html#getStatusCode()