= ApacheCXF =
http://cxf.apache.org/
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
}}}
{{{#!highlight sh
mkdir -p /tmp/cxfSpringTest
cd /tmp/cxfSpringTest
mkdir -p src/main/webapp/WEB-INF/
mkdir -p src/main/java/com/test/
}}}
=== pom.xml ===
{{{#!highlight xml
4.0.0
com.test
cxfSpringTest
0.1
war
org.apache.cxf
cxf-rt-core
2.4.1
org.apache.cxf
cxf-bundle-jaxrs
2.4.1
org.apache.cxf
cxf-rt-frontend-jaxws
2.4.1
}}}
=== src/main/java/com/test/Calculator.java ===
{{{#!highlight java
package com.test;
public class Calculator implements ICalculator
{
public Calculator() {
System.out.println("Calculator created ");
}
public long add(long num1, long num2) {
return (num1 + num2);
}
public long subtract( long num1, long num2 ){
return num1 - num2;
}
}
}}}
=== src/main/java/com/test/ICalculator.java ===
{{{#!highlight java
package com.test;
import javax.jws.WebService;
@WebService
public interface ICalculator {
public long add(long num1 , long num2 );
public long subtract(long num1, long num2 );
}
}}}
=== src/main/webapp/WEB-INF/applicationContext.xml ===
{{{#!highlight xml
}}}
=== nano src/main/webapp/WEB-INF/web.xml ===
{{{#!highlight xml
CXFServlet
org.apache.cxf.transport.servlet.CXFServlet
CXFServlet
/*
org.springframework.web.context.ContextLoaderListener
Spring
org.springframework.web.servlet.DispatcherServlet
0
}}}
=== src/main/java/com/test/TestService.java ===
{{{#!highlight java
package com.test;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("/testSvc")
public class TestService {
@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
String out = String.format("testSvc returns %s", msg);
return Response.status(200).entity(out).build();
}
}
}}}
{{{#!highlight sh
mvn clean compile package # builds cxfSpringTest-0.1.war
# Test links:
# http://localhost:8080/cxfSpringTest-0.1/services
# http://localhost:8080/cxfSpringTest-0.1/testSvc/1001
# Generate client:
wsimport -d src/main/java -keep -p com.test.client http://localhost:8081/cxfSpringTest-0.1/calculator?wsdl
}}}
=== com/test/client/Client.java ===
{{{#!highlight java
/*
cd /tmp/cxfSpringTest/src/main/java
javac com/test/client/*.java
java -cp . com.test.client.Client
AAAA
5
*/
package com.test.client;
public class Client{
public static void main(String args[] ){
System.out.println("AAAA");
CalculatorService cs = new CalculatorService();
ICalculator ic = cs.getCalculatorPort();
long res = ic.add(2,3);
System.out.println(res);
}
}
}}}
== Get project from GitHub ==
{{{#!highlight sh
git clone https://github.com/vborrego/cxf-test
cd cxf-test
mvn clean install
# http://localhost:8081/cxf-test-0.1/calculator?wsdl
# http://localhost:8081/cxf-test-0.1/services
# http://localhost:8081/cxf-test-0.1/testSvc/1001
# http://localhost:8081/cxf-test-0.1/?_wadl&_type=xml # save as app.wadl
}}}
== Generate client ==
{{{#!highlight sh
wget http://search.maven.org/remotecontent?filepath=org/jvnet/ws/wadl/wadl-dist/1.1.6/wadl-dist-1.1.6-bin.zip
cp wadl-dist-1.1.6-bin.zip /tmp/
cd /tmp/
unzip wadl-dist-1.1.6-bin.zip
cd wadl-dist-1.1.6
cd bin
export JAVA_HOME=/opt/java
./wadl2java
./wadl2java -o . -p test app.wadl
}}}
== JAX-WS Response.status ==
* http://docs.oracle.com/javaee/6/api/javax/ws/rs/core/Response.Status.html
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()
== CXF sample WSSE ==
* http://svn.apache.org/viewvc/cxf/trunk/distribution/src/main/release/samples/ws_security/ut/
* https://github.com/apache/cxf/tree/master/distribution/src/main/release/samples/ws_security/ut