spring+cxf简单实现restful web serivce
2013-10-23 11:15
357 查看
一、依赖包
<!-- restful web service begin -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId> <!-- web service -->
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</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-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency> <!-- no declaration can be found for element 'jaxrs:server' -->
<!-- restful web service end -->
二、cxf配置文件
<?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:jaxws="http://cxf.apache.org/jaxws"
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://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="serviceContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="userService" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
三、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-em.xml;classpath:applicationContext-cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- cxf -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
四、java接口
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/userService")
public interface UserService {
@GET
@Path("getUserById/{id}")
@Produces(MediaType.APPLICATION_XML)
UserDTO getUserById(@PathParam("id") int id);
@POST
@Path("addUser")
@Produces(MediaType.APPLICATION_XML)
String addUser(UserDTO dto);
}
五、类实现
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
public UserDTO getUserById(int id) {
System.out.println("ok");
UserDTO user = new UserDTO();
user.setId(1);
user.setPassword("ppt");
user.setUsername("ppt");
return user;
}
@POST
@Path("addUser")
@Produces(MediaType.APPLICATION_XML)
String addUser(UserDTO dto);
}
六、dto
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class UserDTO {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
七、访问
1、直接在浏览器地址栏输入:http://localhost:8888/bbginterface/rest/userService/getUserById/12
2、httpclient方式访问:
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
String uri = "http://localhost:8888/bbginterface/rest/userService/getUserById/12";
GetMethod method = new GetMethod(uri);
client.executeMethod(method);
if(method.getStatusCode() == HttpStatus.SC_OK) {
String content = method.getResponseBodyAsString();
System.out.println(content);
}
}
3、另一种访问方式
1)通过代理生产对象实例,交给spring管理
public class MyProxy implements BeanFactoryPostProcessor{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String baseAddress = "http://127.0.0.1:8888/bbginterface/rest";
UserService userService = JAXRSClientFactory.create(baseAddress, UserService.class);
beanFactory.registerSingleton("userServiceTest", userService);
}
}
2)spring 配置
<bean class="com.csair.proxy.MyProxy"></bean>
<bean id="testJaxreCreation" class="com.csair.test.TestJaxreCreation">
<property name="userServiceTest" ref="userServiceTest"></property>
</bean>
3)测试
public class TestJaxreCreation {
private UserService userServiceTest;
public static void main(String[] args) {
String path = "applicationContext-test-cxf.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);
TestJaxreCreation test = context.getBean("testJaxreCreation", TestJaxreCreation.class);
UserDTO dto = test.userServiceTest.getUserById(12);
System.out.println(dto.toString());
}
}
<!-- restful web service begin -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId> <!-- web service -->
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</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-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency> <!-- no declaration can be found for element 'jaxrs:server' -->
<!-- restful web service end -->
二、cxf配置文件
<?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:jaxws="http://cxf.apache.org/jaxws"
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://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.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-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="serviceContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="userService" />
</jaxrs:serviceBeans>
</jaxrs:server>
</beans>
三、web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>web</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-em.xml;classpath:applicationContext-cxf.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- cxf -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>
四、java接口
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/userService")
public interface UserService {
@GET
@Path("getUserById/{id}")
@Produces(MediaType.APPLICATION_XML)
UserDTO getUserById(@PathParam("id") int id);
@POST
@Path("addUser")
@Produces(MediaType.APPLICATION_XML)
String addUser(UserDTO dto);
}
五、类实现
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
public UserDTO getUserById(int id) {
System.out.println("ok");
UserDTO user = new UserDTO();
user.setId(1);
user.setPassword("ppt");
user.setUsername("ppt");
return user;
}
@POST
@Path("addUser")
@Produces(MediaType.APPLICATION_XML)
String addUser(UserDTO dto);
}
六、dto
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class UserDTO {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
七、访问
1、直接在浏览器地址栏输入:http://localhost:8888/bbginterface/rest/userService/getUserById/12
2、httpclient方式访问:
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
String uri = "http://localhost:8888/bbginterface/rest/userService/getUserById/12";
GetMethod method = new GetMethod(uri);
client.executeMethod(method);
if(method.getStatusCode() == HttpStatus.SC_OK) {
String content = method.getResponseBodyAsString();
System.out.println(content);
}
}
3、另一种访问方式
1)通过代理生产对象实例,交给spring管理
public class MyProxy implements BeanFactoryPostProcessor{
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String baseAddress = "http://127.0.0.1:8888/bbginterface/rest";
UserService userService = JAXRSClientFactory.create(baseAddress, UserService.class);
beanFactory.registerSingleton("userServiceTest", userService);
}
}
2)spring 配置
<bean class="com.csair.proxy.MyProxy"></bean>
<bean id="testJaxreCreation" class="com.csair.test.TestJaxreCreation">
<property name="userServiceTest" ref="userServiceTest"></property>
</bean>
3)测试
public class TestJaxreCreation {
private UserService userServiceTest;
public static void main(String[] args) {
String path = "applicationContext-test-cxf.xml";
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(path);
TestJaxreCreation test = context.getBean("testJaxreCreation", TestJaxreCreation.class);
UserDTO dto = test.userServiceTest.getUserById(12);
System.out.println(dto.toString());
}
}
相关文章推荐
- 使用CXF与Spring集成实现RESTFul WebService
- 用Spring+cxf 实现一个简单的restful webService
- webservice之cxf实现[web项目中基于maven与spring整合]
- Spring Web MVC实现Restful Web Service
- web项目取得spring配置的数据源的简单实现方法
- 使用CXF+spring+restful创建一个web的接口项目
- CXF实现简单webservice应用、集成spring发布到tomcat
- Jersery——RESTful Web Services in Java,RESTful服务,Jersey+Spring实现REST服务调用
- cxf集成到spring中发布restful webservice
- 深入剖析Spring Web源码(十三) - 处理器映射,处理器适配器以及处理器的实现 - 处理器的实现架构 - 简单控制器
- spring 对 timer 的支持 实现简单的web中定时器操作
- 实现简单的spring 管理WebServer功能(CXF版)
- spring + springmvc + cxf 实现webservice restful
- spring boot + webSocket 实现简单会话与在线人数统计的demo
- 简单的Jax-WS WebService实现-cxf
- 实现简单的spring 管理WebServer功能(CXF版)
- 使用CXF+spring+restful创建一个web的接口项目
- CXF+Spring+Hibernate实现RESTful webservice服务端实例
- 在同一个系统里用cxf 实现SOAP 协议和RESTful风格 两种类型的webservice接口(含简单demo)
- 使用CXF与Spring集成实现RESTFul WebService