springmvc+rest整合redis
最近在做一个项目需要用到关系数据库mysql和缓存redis,以及非关系型数据库mongoDB。昨天下午到今天上午一直在搞springmvc整合redis,期间出现的错误一直让人抓狂,在网上搜索的结果也没有解决得了自己的问题,因此整理此文希望可以帮助到和我一样为整合redis而抓狂的人。
首先,在这里说明springmvc整合redis所需要的jar包,commons-pool-1.6.jar、commons-pool2-2.4.2.jar、jedis-2.9.0.jar、spring-data-commons-1.8.6.RELEASE.jar、spring-data-redis-1.8.6.RELEASE.jar。这里主要用到了这些jar包,期间遇到的jar包冲突问题也是醉醉的了,一直在倒腾jar包,最后终于使用以上几个jar包成功了。
以下用代码告诉大家我是怎么做的
1、redis.properties
redis.ip=127.0.0.1
redis.port=6379
redis.pool.maxActive=100
redis.pool.maxIdle=300
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
2、redis-config.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"
default-autowire="byName" default-lazy-init="true"> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.ip}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
</bean>
</beans>
3、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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
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.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
default-autowire="byName"> <!-- 启动annotation支持 -->
<context:annotation-config />
<context:component-scan base-package="com.fp.daoImpl,com.fp.serviceImpl,com.fp.rest"></context:component-scan>
<mvc:annotation-driven/>
<!-- <util:properties id="jdbcProps" location="classpath:jdbc.properties"></util:properties> --> <context:property-placeholder location="classpath:jdbc.properties,classpath:redis.properties" /> <!-- 导入redis的配置文件 -->
<import resource="redis-config.xml"/> <!-- 配置mysql数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置jdbctemplate,并为其注入dataSource数据源 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> </beans>
4、jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/xxx?useUnicode=true&characterEncoding=utf8
jdbc.username = root
jdbc.password =root
5、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JerseyProject</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</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.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener> <!-- 使用rest风格-->
<servlet>
<servlet-name>restService</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.fp.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>restService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
6、模块代码
Member.java
package com.fp.entity; import javax.xml.bind.annotation.XmlRootElement; import com.ems.base.BaseModel; @XmlRootElement
public class Member extends BaseModel{ private static final long serialVersionUID = -1959528436584592183L;
private String id;
private String nickname; public Member(){} public Member(String id, String nickname){
this.setId(id);
this.setNickname(nickname);
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getNickname() {
return nickname;
} public void setNickname(String nickname) {
this.nickname = nickname;
} }
MemberDao
package com.fp.dao;
import com.fp.entity.Member;
public interface MemberDao {
Member get(String keyId);
}
MemberDaoImpl
package com.fp.daoImpl; import java.io.Serializable; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Repository; import com.fp.dao.MemberDao;
import com.fp.entity.Member; @Repository(value = "memberDao")
public class MemberDaoImpl implements MemberDao { @Autowired
private RedisTemplate<Serializable, Object> redisTemplate; @Resource
public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
} /**
* 读取缓存
*
* @param key
* @return
*/
public Member get(final String keyId) {
Member result = redisTemplate.execute(new RedisCallback<Member>() {
public Member doInRedis(RedisConnection connection)
throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] key = serializer.serialize(keyId);
byte[] value = connection.get(key);
if (value == null) {
return null;
}
String nickname = serializer.deserialize(value);
System.out.println("nickname::"+nickname);
return new Member(keyId, nickname);
}
});
return result;
}
}
MemberService
package com.fp.service;
import com.fp.entity.Member;
public interface MemberService {
public Member get(String id);
}
MemberServiceImpl
package com.fp.serviceImpl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.fp.dao.MemberDao;
import com.fp.entity.Member;
import com.fp.service.MemberService; @Service("memberService")
public class MemberServiceImpl implements MemberService { @Autowired
private MemberDao memberDao; public Member get(String id) {
return memberDao.get(id);
}
}
MemberResource(提供rest服务)
package com.fp.rest; import com.fp.entity.Member;
import com.fp.service.MemberService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController; @RestController
@Path("/member")
public class MemberResource { @Autowired
private MemberService memberService; @Path("/{id}")
@GET //表示此服务路径基于get请求模式
@Produces(MediaType.APPLICATION_JSON+";charset=utf-8") //表示响应的结果以文本方式返回
public Member get(@PathParam("id") String id){
return memberService.get(id);
}
}
7、运行结果
访问地址:http://localhost:8080/JerseyProject/member/id,运行结果如下:

到此springmvc+rest+redis就整合完毕了,重要的事情再强调一遍,一定要导入版本相符的jar包,commons-pool-1.6.jar、commons-pool2-2.4.2.jar、jedis-2.9.0.jar、spring-data-commons-1.8.6.RELEASE.jar、spring-data-redis-1.8.6.RELEASE.jar。相信你一定可以的,嘿嘿。。。
springmvc+rest整合redis的更多相关文章
- Spring+SpringMVC+Mybatis整合redis
SSM整合redis redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列. 这里用的是ssm框架+maven构建的项目 ...
- springmvc整合redis架构搭建实例
新换环境,又有新东西可以学习了,哈皮! 抽空学习之余看了一下redis,个人对Springmvc的爱是忠贞不渝,所以整理了一下Springmvc整合redis的环境搭建.分享学习. 第一步: 创建ma ...
- Spring+Mybatis基于注解整合Redis
基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...
- SSM之整合Redis
Redis安装与使用 第一步当然是安装Redis,这里以Windows上的安装为例. 首先下载Redis,可以选择msi或zip包安装方式 zip方式需打开cmd窗口,在解压后的目录下运行redis- ...
- Spring整合Redis&JSON序列化&Spring/Web项目部署相关
几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...
- ssm 整合 redis(简单教程)
最后我建议大家使用 Spring StringRedisTemplate 配置,参阅: http://blog.csdn.net/hanjun0612/article/details/78131333 ...
- sping整合redis,以及做mybatis的第三方缓存
一.spring整合redis Redis作为一个时下非常流行的NOSQL语言,不学一下有点过意不去. 背景:学习Redis用到的框架是maven+spring+mybatis(框架如何搭建这边就不叙 ...
- [手把手教程][JavaWeb]优雅的SpringMvc+Mybatis整合之路
来源于:http://www.jianshu.com/p/5124eef40bf0 [手把手教程][JavaWeb]优雅的SpringMvc+Mybatis整合之路 手把手教你整合最优雅SSM框架:S ...
- Spring4+SpringMVC+MyBatis整合思路(山东数漫江湖)
1.Spring框架的搭建 这个很简单,只需要web容器中注册org.springframework.web.context.ContextLoaderListener,并指定spring加载配置文件 ...
随机推荐
- MySQL整理(二)
一.MySQL操作表的约束 MySQL提供了一系列机制来检查数据库表中的数据是否满足规定条件,以此来保证数据库表中数据的准确性和一致性,这种机制就是约束. (1)设置非空约束(NOT NULL),唯一 ...
- Objective-C中的alloc和init问题
从开始学的NSString *name=[[NSString alloc] init] 起,仅仅这句话是分配内存空间,一直在用,从来没考虑过它的内部是怎么实现的.今天无意中看到了这一句代码: NSSt ...
- 11、classmethod和staticmethod
类中定义的函数有两大类(3小种)用途,一类是绑定方法,另外一类是非绑定方法 1. 绑定方法:特点:绑定给谁就应该由谁来调用,谁来调用就会将谁当作第一个参数自动传入1.1 绑定给对象的:类中定义的函数默 ...
- vue-cli中的.babelrc文件介绍
转载自:http://www.cnblogs.com/ye-hcj/p/7071850.html { // 此项指明,转码的规则 "presets": [ //个人认为多此一举 [ ...
- Salesforce中通过SOAP API和Metadata API开发java的web server服务
1.下载Salesforce平台中WSDL文件 在Salesforce中创建了自己需要用到的对象后,我们想要在别的应用中读写纪录到对象中,首先需要的是自己Salesforce平台的权限通过.登陆自己的 ...
- cocos2d-x 2.x.x 新建工程 android下的 org文件夹丢失
cocos2d-x 2.x.x 新建工程之后... 打开android项目..会发现src下没有org文件... 解决方法一: cocos2d-x-2.2.0\cocos2dx\platform\an ...
- iOS 截屏,openGL ES 截图,以及像素颜色判断
代码整理了2种截图,类似.(没苹果自带那种截图彻底) 方法一: +(UIImage *)fullScreenshots{ UIWindow *screenWindow = [[UIApplicatio ...
- @RequestMapping @SessionAttributes @ModelAttribute注解用法
简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...
- centos安装xdebug 和 phpstorm+Xdebug断点调试PHP
转载地址:http://www.2cto.com/os/201304/206058.html CentOS下安装xdebug 在CentOS 6.x 的系统中,是集成xdebug 的, y ...
- Clonal hematopoiesis of indeterminate potential(意义不明的克隆性造血)-CHIP
意义不明的克隆性造血是指由一个造血干细胞或者其他早期的起始血细胞为了更好的适应环境而发展成一个带有一些基因变异的亚型. 这个亚型带有基因变异一般是非驱动性的,而且该亚型占血细胞的比率跟年龄有很大的相关 ...