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.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>redis-shorterurl</artifactId> <version>0.0.1-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
Author:LuffyStory
Origin:http://www.cnblogs.com/luffystory/