大家简单体会一下
首先肯定是常规的创建springboot项目,
第二要加依赖,集成了redis肯定要加redis依赖
<?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 https://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.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.bipowernode.redis</groupId>
<artifactId>0418-springboot-redis</artifactId>
<version>1.0.0</version>
<name>0418-springboot-redis</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--集成redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<!-- spring-boot-starter-data-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
依赖文件大家注意看注释掉的位置,我用的springboot是1.4的版本,要把
spring-boot-starter-redis改成
spring-boot-starter-data-redis否则出现警告
然后写控制层,在web包下
@RestController
public class Studentacontroller {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/put")
public Object put(String key,String value){
studentService.put(key,value);
return "成功";
}
}
接口还有接口调用的方法
接下来写接口和实现类这些都不用多说,大家早就应该熟悉
package com.bipowernode.redis.service;
public interface StudentService {
void put(String key,String value);
}
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@Override
public void put(String key, String value) {
if(key!=null&&value!=null)
redisTemplate.opsForValue().set(key,value);
}
}
其中Servce注解一定家的家长,如果没有加上就会爆出错误信息如下
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: non null key required] with root cause
解决方法如下
以此检查Controller层是否加ResponseBody
Service层是否加注解Autowired
RestCotroller使用成了Controller
最后我出现的错误是(已经在前面改过来了,就是判空那一步)
non null key required]
在浏览器里面输入http://localhost:8080/put
简单的集成,体会一下就可以,redis不会的可以补一下redis