Springboot 连接 使用 Redis Example
通过一个简单的例子使用Springboot 连接并使用Redis。 本文假设已经安装好Redis。
1.首先将URL转换为一个ID ,并使用 StringRedisTemplate 将ID 和 URL 保存到Redis
2. 根据ID 从Redis中获取对应的URL
项目结构如下

POM文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>redis-shorterurl</artifactId>
<version>0.0.-SNAPSHOT</version>
<name>redis-shorterurl</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
以及Java文件
package com.example.redisshorterurl; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class RedisShorterurlApplication { public static void main(String[] args) {
SpringApplication.run(RedisShorterurlApplication.class, args);
} }
package com.example.redisshorterurl; import java.nio.charset.StandardCharsets; import org.apache.commons.validator.routines.UrlValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import com.google.common.hash.Hashing; @RestController
public class URLShorterResource { @Autowired
StringRedisTemplate redisTemplate; @GetMapping("URL/{id}")
public String getUrl(@PathVariable String id) {
String url = redisTemplate.opsForValue().get(id);
System.out.println("URL Retrieved: " + url); if (url == null) {
throw new RuntimeException("There is no shorter URL for : " + id);
}
return url;
} @PostMapping("/create")
public String create(@RequestBody String url) {
UrlValidator urlValidator = new UrlValidator(new String[] { "http", "https" });
if (urlValidator.isValid(url)) {
String id = Hashing.murmur3_32().hashString(url, StandardCharsets.UTF_8).toString();
System.out.println("URL ID generated:" + id);
redisTemplate.opsForValue().set(id, url);
return id;
}
throw new RuntimeException("URL Invalid: " + url); } }
application.properties

之后启动Redis server

启动Springboot Application.
此时,可以用Postman测试效果。
首先, 使用 HTTP POST method 根据URL 生成一个 ID

之后,使用 ID 获取对应的 URL

Springboot 连接 使用 Redis Example的更多相关文章
- springboot连接redis进行CRUD
springboot连接redis进行CRUD: 1.添加以下依赖: <dependency> <groupId>org.springframework.boot</gr ...
- springboot连接redis错误 io.lettuce.core.RedisCommandTimeoutException:
springboot连接redis报错 超时连接不上 可以从以下方面排查 1查看自己的配置文件信息,把超时时间不要设置0毫秒 设置5000毫秒 2redis服务长时间不连接就会休眠,也会连接不上 重 ...
- 不会用SpringBoot连接Redis,那就赶紧看这篇
摘要:如何通过springboot来集成操作Redis. 本文分享自华为云社区<SpringBoot连接Redis操作教程>,作者: 灰小猿. 今天来和大家分享一个如何通过springbo ...
- springboot+redis+虚拟机 springboot连接linux虚拟机中的redis服务
文章目录 1.前提条件:确保虚拟机开启.并且连接到redis 2.新建立一个springboot项目,创建项目时勾选web选项 3.在pom中引入redis依赖 4.在application.prop ...
- springboot 连接redis
引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...
- 由浅入深学习springboot中使用redis
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
- 基于springboot+bootstrap+mysql+redis搭建一套完整的权限架构【六】【引入bootstrap前端框架】
https://blog.csdn.net/linzhefeng89/article/details/78752658 基于springboot+bootstrap+mysql+redis搭建一套完整 ...
- SpringBoot中集成redis
转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...
- SpringBoot入门 (七) Redis访问操作
本文记录学习在SpringBoot中使用Redis. 一 什么是Redis Redis 是一个速度非常快的非关系数据库(Non-Relational Database),它可以存储键(Key)与 多种 ...
随机推荐
- NYOJ781 又见回文数
又见回文数 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 冷淡的回文数被水了,各种被水,然后他非常生气,然后... 一个数从左边读和从右边读一样,就说这个数是回文数 ...
- uva 11346 - Probability(可能性)
题目链接:uva 11346 - Probability 题目大意:给定x,y的范围.以及s,问说在该范围内选取一点,和x,y轴形成图形的面积大于s的概率. 解题思路:首先达到方程xy ≥ s.即y ...
- 【v2.x OGE课程 15】 布局相关
1.父亲和儿子的关系 我们可以Entity类看到非常多parent(父)与child(子)这种字眼,这是游戏引擎中常有的概念,简单而言是一种has-a的关系 watermark/2/text/aHR0 ...
- wpf C# 操作DirectUI窗口 SendMessage+MSAA
原文:wpf C# 操作DirectUI窗口 SendMessage+MSAA 最近做一个抓取qq用户资料的工具,需要获取qq窗口上的消息,以前这种任务是用句柄获取窗口中的信息,现在qq的窗口用的是D ...
- 【转载】使用Docker Hub官方gcc:latest镜像编译C/C++程序以及缩小镜像的方法
摘要:使用Docker Hub官方gcc:latest镜像(1.2GB)编译C/C++程序,以及缩小镜像的方法. 方法1: 在gcc容器里编译C/C++程序 将C/C++代码运行在gcc容器内的最简单 ...
- 【转载】动态载入DLL所需要的三个函数详解(LoadLibrary,GetProcAddress,FreeLibrary)
原文地址:https://www.cnblogs.com/westsoft/p/5936092.html 动态载入 DLL 动态载入方式是指在编译之前并不知道将会调用哪些 DLL 函数, 完全是在运行 ...
- 改善C#程序的建议7:正确停止线程
原文:改善C#程序的建议7:正确停止线程 开发者总尝试对自己的代码有更多的控制.“让那个还在工作的线程马上停止下来”就是诸多要求中的一种.然而事与愿违,这里面至少存在两个问题: 第一个问题是:正如线程 ...
- 企业级架构 MVVM 模式指南 (WPF 和 Silverlight 实现) 译(1)
前言对于WPF和Silverlight来讲,MVVM是微软设计师和业内专家高度推荐的非常棒的一种设计模式.本书会探讨MVVM设计模式的一些自身缺陷以及为什么MVVM还不能成为行业内的标准设计模式.这会 ...
- 毕设(一)C#的百度api调用
这个学期就要毕业了,选了一个无人机地面站软件设计的题目,这几天也开始着手做, 首先做了一个百度地图的调用,这里因为是上位机的开发,所有就不介绍Javascript的 调用方法,核心是用到一个类Http ...
- LINUX基础内容
在Linux中,有三种基本的文件类型: 1) 普通文件 普通文件是以字节为单位的数据流,包括文本文件.源码文件.可执行文件等.文本和二进制对Linux来说并无区别,对普通文件的解释由处理该文件的应用程 ...