CXF+Spring+JAXB+Json构建Restful服务
话不多说,先看详细的样例:
文件文件夹结构:
web.xml
<?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">
<display-name>platform</display-name>
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此參数用于后面的Spring Context
Loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml,classpath:/applicationContext-rs.xml
</param-value>
</context-param> <!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter> <filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- Apache CXF Servlet 配置 -->
<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>
</web-app>
Spring的xml—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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-lazy-init="true">
<description>Spring公共配置文件</description> <!-- 使用annotation 自己主动注冊bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="com.zd.daoImpl.*,com.zd.beanImpl.*,com.zd.serviceImpl.*,com.zd.service.*">
</context:component-scan>
<bean id="dozerMapper" class="org.dozer.DozerBeanMapper"
lazy-init="false" /> <!-- Matrix BPM -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
</props>
</property>
</bean>
</beans>
RestFul的xml—applicationContext-rs.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:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <description>Rails CM 的全部RESTFUL 服务配置</description> <context:component-scan base-package="com.zd" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"></bean> <jaxrs:server id="restContainer" address="/">
<jaxrs:inInterceptors>
</jaxrs:inInterceptors>
<jaxrs:serviceBeans>
<ref bean="userinfoServiceImpl" />
</jaxrs:serviceBeans>
</jaxrs:server> </beans>
实体:
package com.zd.entity;
import java.io.Serializable; /** 系统用户表 **/
public class UserInfo implements Serializable { private String id;
private String name;// 用户姓名
private String sex;// 性别
private String idNo;//身份证号码
private String account;// 账号
private String pwd;// password public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getPwd() {
return pwd;
} public void setPwd(String pwd) {
this.pwd = pwd;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getIdNo() {
return idNo;
} public void setIdNo(String idNo) {
this.idNo = idNo;
}
}
构建的XML实体:
package com.zd.dtd; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; @XmlRootElement(name="UserInfoDTO")
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlType
public class UserInfoDTD {
private String id;
private String name;// 用户姓名
private String sex;// 性别
private String idNo;//身份证号码
private String account;// 账号
private String pwd;// password
@XmlElement(name="id",required=false)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name="name",required=false)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name="account",required=false)
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
@XmlElement(name="sex",required=false)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@XmlElement(name="sex",required=false)
public String getIdNo() {
return idNo;
}
public void setIdNo(String idNo) {
this.idNo = idNo;
}
@XmlElement(name="sex",required=false)
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
接口服务层:
package com.zd.service; import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; @Path("/Userinfo")
public interface UserinfoService { /**
* 依据id获取用户信息
* @param id 用户id
* @return json数据
*/
@GET
@Path("/getUserinfo")
@Produces({ "text/html;charset=utf-8", MediaType.TEXT_HTML })
public Response getBasicUserInfo(@QueryParam("id") String id);
}
服务实现层:
package com.zd.serviceImpl; import javax.ws.rs.core.Response; import net.sf.json.JSONArray; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import com.zd.beanImpl.UserBeanImpl;
import com.zd.dtd.UserInfoDTD;
import com.zd.entity.UserInfo;
import com.zd.service.UserinfoService; @Component
public class UserinfoServiceImpl implements UserinfoService { @Autowired(required=true)
private UserBeanImpl userBeanImpl; @Override
public Response getBasicUserInfo(String id) { UserInfo userInfo = userBeanImpl.getUserInfo(id); UserInfoDTD userDTD=new UserInfoDTD();
userDTD.setSex(userInfo.getSex());
userDTD.setId(userInfo.getId());
userDTD.setIdNo(userInfo.getIdNo());
userDTD.setName(userInfo.getName());
userDTD.setPwd(userInfo.getPwd());
userDTD.setAccount(userInfo.getAccount());
//json转换
JSONArray jsonArray = JSONArray.fromObject(userInfo);
return Response.ok(jsonArray.toString()).build();
}
}
ServiceBean:
package com.zd.beanImpl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.zd.daoImpl.UserinfoDaoImpl;
import com.zd.entity.UserInfo; @Service
//默认将类中的全部函数纳入事务管理.
@Transactional
public class UserBeanImpl {
@Autowired(required=true)
private UserinfoDaoImpl userinfoDaoImpl;
public UserInfo getUserInfo(String id){
UserInfo userInfo=userinfoDaoImpl.getUserinfo(id);
return userInfo; }
}
Dao:
package com.zd.daoImpl;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional; import com.zd.entity.UserInfo;
@Repository
//默认将类中的全部函数纳入事务管理.
@Transactional
public class UserinfoDaoImpl {
public UserInfo getUserinfo(String id){
UserInfo userInfo=new UserInfo();
userInfo.setAccount("zhudan");
userInfo.setId("1111");
userInfo.setId("520911188119186770");
userInfo.setName("朱丹");
userInfo.setPwd("1221");
userInfo.setSex("女");
return userInfo;
}
}
页面訪问地址:
http://localhost:8080/restful/rest/Userinfo/getUserinfo
页面訪问结果:
总结:
先由这个样例。宏观了解下Restful的服务搭建。Restful的细节东西再在以后的运用中深入理解
CXF+Spring+JAXB+Json构建Restful服务的更多相关文章
- SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务
一.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- Springboot & Mybatis 构建restful 服务五
Springboot & Mybatis 构建restful 服务五 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务四 2 restful ...
- Springboot & Mybatis 构建restful 服务二
Springboot & Mybatis 构建restful 服务二 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务一 2 restful ...
- Springboot & Mybatis 构建restful 服务
Springboot & Mybatis 构建restful 服务一 1 前置条件 jdk测试:java -version maven测试:命令行之行mvn -v eclipse及maven插 ...
- 用Jersey构建RESTful服务7--Jersey+SQLServer+Hibernate4.3+Spring3.2
一.整体说明 本例执行演示了用 Jersey 构建 RESTful 服务中.怎样集成 Spring3 二.环境 1.上文的项目RestDemo 2.Spring及其它相关的jar ,导入项目 三.配置 ...
- jersey+maven构建restful服务
一.新建一个Maven Web项目 a) 新建一个简单的Maven项目 b) 将简单的Maven项目转成Web项目 (若没出现further configuration available--或里面的 ...
- Springboot & Mybatis 构建restful 服务四
Springboot & Mybatis 构建restful 服务四 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务三 2 restful ...
- Springboot & Mybatis 构建restful 服务三
Springboot & Mybatis 构建restful 服务三 1 前置条件 成功执行完Springboot & Mybatis 构建restful 服务二 2 restful ...
- 使用spring mvc或者resteasy构建restful服务
看到最近一个项目里用resteasy来构建restful接口,有点不明白,不少Spring mvc4.0以后也可以很方面的实现restful服务吗,为啥还要在Spring MVC的项目里还引入rest ...
随机推荐
- CF 463D Gargari and Permutations [dp]
给出一个长为n的数列的k个排列(1 ≤ n ≤ 1000; 2 ≤ k ≤ 5).求这个k个数列的最长公共子序列的长度 dp[i]=max{dp[j]+1,where j<i 且j,i相应的字符 ...
- spark streaming的理解和应用
1.Spark Streaming简介 官方网站解释:http://spark.apache.org/docs/latest/streaming-programming-guide.html 该博客转 ...
- android 上传图片
public static String uploadPicture(String url, String uploadFile) { String resultcode = "1& ...
- POJ2762 Going from u to v or from v to u? 强连通+缩点
题目链接: poj2762 题意: 给出一幅单向图.问这张图是否满足 随意两点ab 都能 从a到达b 或 从b到达a 题解思路: 推断一幅图是否满足弱连通 首先想到的是将图中的 强连通分量(能互 ...
- 有关String的转换的一篇好文章
Pay Close Attention - String Handling I need to make a detour for a few moments, and discuss how t ...
- 设置网站expires和max-age属性
转:http://www.zicheng.net/article/982022.htm 在使用百度站长工具测试网站优化建议时,在 设置静态内容缓存时间 栏目里,会提示 类似 FAILED - (未设置 ...
- 压缩 MongoDB 的数据文件
MongoDB采用了磁盘空间预分配的机制,为了避免磁盘碎片以及使用mmap后造成的近一步的内存碎片,但是随着数据的增删除改操作,数据文件不可避免的会产生空洞,造成磁盘空间和内存的浪费.本文说的是这方面 ...
- css选择器顺序的小技巧
在线演示 本地下载 css的选择器的顺序其实很有意思,如果应用的好的话,可以做一些简单的逻辑出来,配合上css3,就可以尽可能的脱离js了. 这里的演示是一个带有hover事件的四张照片,效果来自一个 ...
- 高并发分布式环境中获取全局唯一ID[分布式数据库全局唯一主键生成]
需求说明 在过去单机系统中,生成唯一ID比较简单,可以使用MySQL的自增主键或者Oracle中的sequence, 在现在的大型高并发分布式系统中,以上策略就会有问题了,因为不同的数据库会部署到不同 ...
- 浅谈JavaScript框架设计
在这个js框架随处乱跑的时代,你是否考虑过写一个自己的框架?下面的内容也许会有点帮助. 一个框架应该包含哪些内容? 1.语言扩展 大部分现有的框架都提供了这部分内容,语言扩展应当是以ECMAScrip ...