redis+java+put+api_SpringBoot整合Redis开发RESTful API接口

开发环境

SpringBoot版本:2.0.5.RELEASE、jdk版本:1.8+、Maven版本:3.5.4

项目框架

├── pom.xml

├── springbootreidsrestfulapi.iml

├── src

│ ├── main

│ │ ├── java

│ │ │ └── com

│ │ │ └── jenkin

│ │ │ └── springboot

│ │ │ ├── App.java

│ │ │ ├── controller

│ │ │ │ └── UserController.java

│ │ │ ├── pojo

│ │ │ │ ├── RedisConfigBean.java

│ │ │ │ └── User.java

│ │ │ ├── redis

│ │ │ │ └── RedisConfig.java

│ │ │ └── service

│ │ │ ├── UserService.java

│ │ │ └── impl

│ │ │ └── UserServiceImpl.java

│ │ └── resources

│ │ └── application.properties

配置文件

在SpringBoot里的application.properties文件中配置Redis信息

# REDIS (RedisProperties)

# Database index used by the connection factory.

spring.redis.database=0

# Redis server host.

spring.redis.host=localhost

# Login password of the redis server.

spring.redis.password=

# Redis server port.

spring.redis.port=6379

# Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit.

spring.redis.lettuce.pool.max-active=8

# Maximum number of "idle" connections in the pool. Use a negative value to indicate an unlimited number of idle connections.

spring.redis.lettuce.pool.max-idle=8

# Maximum amount of time a connection allocation should block before throwing an exception when the pool is exhausted. Use a negative value to block indefinitely.

spring.redis.lettuce.pool.max-wait=-1ms

# Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if it is positive.

spring.redis.lettuce.pool.min-idle=0

# Shutdown timeout.

spring.redis.lettuce.shutdown-timeout=100ms

Redis有两种连接方式:一个是Jedis,另一个是Lettuce。它俩间的区别:使用Jedis,如果多线程使用同一个连接,线程时不安全的,需要使用连接池,为每一个Jedis实例分配一个连接;而使用Lettuce,当多线程连接同一个连接实例时,是线程安全的。

SpringBoot2.0以后,默认的连接方式是Lettuce,我这里使用的也是Lettuce,首先添加Redis包:

org.springframework.boot

spring-boot-starter-data-redis

如果使用的是Jedis连接,需要修改一下配置:

org.springframework.boot

spring-boot-starter-data-redis

io.lettuce

lettuce-core

redis.clients

jedis

建立连接

/**

* 使用Lettuce连接Redis

* @return

*/

@Bean

public LettuceConnectionFactory connectionFactory() {

LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory();

lettuceConnectionFactory.setDatabase(redisConfigBean.getDatabase());

lettuceConnectionFactory.setHostName(redisConfigBean.getHost());

lettuceConnectionFactory.setPassword(redisConfigBean.getPassword());

lettuceConnectionFactory.setPort(redisConfigBean.getPort());

return lettuceConnectionFactory;

}

效果演示

POST请求:设置ContentType=application/json,添加RequestBody中的内容,格式为Json格式

e957a2dc5de0

POST请求

GET请求

e957a2dc5de0

GET请求

PUT请求:设置ContentType=application/json,修改RequestBody中的内容,格式为Json格式

e957a2dc5de0

PUT请求

e957a2dc5de0

GET请求

DELETE请求

e957a2dc5de0

DELETE请求

e957a2dc5de0

GET请求

代码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值