Apache CXF
特徴:
1.コード量が少ない
2.Springと相性がよい
3.DataBindingが豊富
web.xml(Apache CXF + Spring)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5" >
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
</web-app>
mavenのpom.xml
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-aegis</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
サンプル(JAX-RS + Spring)
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>2.5.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle-jaxrs</artifactId>
<version>2.5.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.6.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
★単体テスト
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import static org.junit.Assert.*;
import org.junit.Test;
@Test
public void testMethod() {
WebClient client = WebClient
.create(SERVER_URL)
.type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON);
Order order = new Order();
order.setOrderNo(1L);
order.setUserName("Andy");
order.setItemName("item01");
//http://localhost:8080/sample/order/create
Response res = client.post(order);
//http://localhost:8080/sample/order/get/1
Response res = client.get();
//http://localhost:8080/sample/order/update
Response res = client.put(order);
//http://localhost:8080/sample/order/cancel/1
Response res = client.delete();
try {
assertEquals(Status.OK.getStatusCode(), res.getStatus());
assertEquals(
"{\"order\":{\"orderNo\":1,\"userName\":\"Andy\",\"itemName\":\"item01\"}}",
IOUtils.readStringFromStream( (InputStream)res.getEntity() ));
} catch (IOException e) {
fail();
}
}
JAX-WS
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
●書き方1
<jaxws:endpoint id="calculate"
implementor="org.xxx.cxf.jaxws.CalculateImpl"
address="/CalculateService" />
●書き方2
<jaxws:endpoint address="/CalculateService">
<jaxws:implementor ref="calculate" /> @Service("calculate")が必要
</jaxws:endpoint>
<context:component-scan base-package="org.xxx.cxf.jaxws" />
</beans>
JAX-RS
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<ref bean="customerServiceImpl" />
<ref bean="xxxServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
<bean id="customerServiceImpl" class="org.xxx.demo.rs.CustomerServiceImpl" />
<bean id="xxxServiceImpl" class="org.xxx.demo.rs.XxxServiceImpl" />
</beans>
applicationContext.xml(Spring 3.0版)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<context:component-scan base-package="jp.sample.xxx" />
<jaxrs:server id="customerService" address="/">
<jaxrs:serviceBeans>
<ref bean="customerServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>