使用Apache CXF搭建REST风格的Web Service
2015-03-06 17:14
375 查看
1)编写domain实体类
2)业务处理接口
3)业务处理实现类
3)Web Service接口
4)Web Service实现类
5)配置applicationContext.xml文件
6)配置web.xml
7)客户端测试
package com.domain; import java.io.Serializable; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="UserInfo") public class User implements Serializable,Comparable<User>{ /** * */ private static final long serialVersionUID = 1L; private int id; private String name; private String email; private String address; public User(){} @XmlElement public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @XmlElement public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return this.name+","+this.email+","+this.address; } @Override public int compareTo(User other) { return this.id - other.id; } }
2)业务处理接口
package com.service; import com.domain.User; public interface IUserService { public User getUserById(int id); }
3)业务处理实现类
package com.service; import com.domain.User; public class UserService implements IUserService{ public User getUserById(int id){ User user = new User(); user.setId(1); user.setName("Rickesy"); user.setAddress("china"); user.setEmail("Rickesy@163.com"); return user; }; }
3)Web Service接口
package com.rest; 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; import com.domain.User; @Path(value="/") public interface IRESTService { @GET @Path("user/{id}") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public User getUserById(@PathParam("id") int id); }
4)Web Service实现类
package com.rest; 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; import com.domain.User; import com.service.UserService; @Path(value="/") public class RESTService implements IRESTService{ private User user; private IUserService userService; @GET @Path("user/{id}") @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) public User getUserById(@PathParam("id") int id){ return userService.getUserById(id); } public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
5)配置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:p="http://www.springframework.org/schema/p" 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-3.1.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.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"/> <bean id="user" class="com.domain.User"></bean> <bean id="userService" class="com.service.UserService"></bean> <bean id="restService" class="com.rest.RESTService"> <property name="user" ref="user"></property> <property name="userService" ref="userService"></property> </bean> <jaxrs:server id="restServiceContainer" address="/rest"> <jaxrs:serviceBeans> <ref bean="restService" /> </jaxrs:serviceBeans> <jaxrs:extensionMappings> <entry key="json" value="application/json" /> <entry key="xml" value="application/xml" /> </jaxrs:extensionMappings> <jaxrs:languageMappings> <entry key="en" value="en-gb"/> </jaxrs:languageMappings> </jaxrs:server> </beans>
6)配置web.xml
<listener> <listene-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <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>/*</url-pattern> </servlet-mapping>
7)客户端测试
package com.client; import javax.ws.rs.core.MediaType; import org.apache.cxf.jaxrs.client.WebClient; public class Client { static WebClient client = WebClient.create( "http://localhost:8080/RESTWebService/rest"); public static void main(String[] args) { User user = client.path("/user/{id}", 1).accept(MediaType.APPLICATION_XML).get(User.class); System.out.println(user); } }
相关文章推荐
- 使用Apache CXF搭建REST风格的Web Service
- 使用Mahout搭建推荐系统之入门篇1-搭建REST风格简单推荐系统
- 使用Apache CXF搭建基于SOAP的Web Service
- 使用Mahout搭建推荐系统之入门篇1-搭建REST风格简单推荐系统
- 使用Spring MVC搭建REST风格的服务
- 简单介绍使用WCF的Web编程模型开发REST风格的Web Service
- 使用Apache CXF搭建基于SOAP的Web Service
- 使用Maven和Jersey Framework开发REST风格Web Service
- 【项目搭建】使用spring-boot进行REST风格的微服务开发入门
- 使用Mahout搭建推荐系统之入门篇1-搭建REST风格简单推荐系统
- 使用Maven和Jersey Framework开发REST风格Web Service
- 简单介绍使用WCF的Web编程模型开发REST风格的Web Service
- 使用Mahout搭建推荐系统之入门篇1-搭建REST风格简单推荐系统
- 使用Mahout搭建推荐系统之入门篇1-搭建REST风格简单推荐系统
- 使用NetKerne实现REST风格的ESB
- 构建REST风格的Web Service
- 利用GAE+RestEasy搭建Web Service---前言
- Apache CXF实战之七 使用Web Service传输文件
- Apache CXF实战之九 发布使用SSL的Web Service
- 使用 sqlRest 将数据库转换为 REST 风格的 Web 服务