我正在开发一个简单的应用程序来学习 Redis 缓存。在我将 Redis 添加为 Spring Boot 项目上的缓存机制之前,API 工作正常。你可以说REST项目。使用Postman来调用不同的API。添加redis后,我发现用于删除数据的
@CacheEvict
工作正常。但是当我调用像GetMapping,PutMapping这样用@Cacheable
注释的请求时,@CachePut
不起作用。我不明白 @CacheEvict
完美工作 @Cacheable
,@CachePut
不工作有什么问题。
这是当我使用以下网址调用 API 时邮递员给出的 错误:
放置:
http://localhost:8080/bank/2/deposit
http://localhost:8080/bank/holderName/Aysha
{
"dateTime": "2024-03-30T16:49:17.7557062",
"message": "Cannot serialize",
"error": "uri=/bank/2/deposit",
"path": "Bad Request"
}
让我更详细地解释一下我是如何配置redis的:
POM:
<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
应用程序属性:
spring.cache.type=redis
spring.cache.redis.time-to-live=60000
spring.cache.redis.host=localhost
spring.cache.redis.port=6379
账户(实体):
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Account implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String holderName;
private double balance;
}
这里我启用了缓存:
@SpringBootApplication
@EnableCaching
public class SimpleBankingApplication {
@Bean
public ModelMapper mapper(){
return new ModelMapper();
}
public static void main(String[] args) {
SpringApplication.run(SimpleBankingApplication.class, args);
}
}
控制器类:
@Controller
@RequestMapping("bank")
@AllArgsConstructor
@CacheConfig(cacheNames = "account")
public class AccountController {
private AccountService service;
@PostMapping("createAccount")
public ResponseEntity<AccountDTO> createAccount(@RequestBody @Valid AccountDTO dto){
return new ResponseEntity<>(service.addAccount(dto), HttpStatus.CREATED);
}
@GetMapping("holderName/{name}")
@Cacheable(value = "account", key = "#name")
public ResponseEntity<AccountDTO> getAccountByName(@PathVariable String name){
return new ResponseEntity<>(service.getAccount(name),HttpStatus.ACCEPTED);
}
@PutMapping("{id}/deposit")
@CachePut(cacheNames = "account", key = "#id")
public ResponseEntity<AccountDTO> deposit(@PathVariable Long id, @RequestBody Map<String, Double> request){
Double value = request.get("balance");
return new ResponseEntity<>(service.deposit(id,value), HttpStatus.ACCEPTED);
}
@PutMapping("{id}/withdraw")
public ResponseEntity<AccountDTO> withdraw(@PathVariable Long id, @RequestBody Map<String, Double> request){
Double value = request.get("withdraw_balance");
return new ResponseEntity<>(service.withdraw(id,value), HttpStatus.ACCEPTED);
}
@GetMapping("allAccounts")
public ResponseEntity<List<AccountDTO>> getAllAccount(){
return new ResponseEntity<>(service.getAllAccounts(),HttpStatus.ACCEPTED);
}
@DeleteMapping("deleteAccount/{id}")
@CacheEvict(cacheNames = "account", key = "#id", beforeInvocation = true)
public ResponseEntity<String> deleteAccount(@PathVariable Long id){
service.delete(id);
return new ResponseEntity<>("Account has been deleted!!!", HttpStatus.ACCEPTED);
}
}
项目链接: 查看项目
问题是控制器类上的注释错误:
@Controller
应该是:
@RestController
后者专用于 REST API 控制器,与 Web 控制器相反。不同之处在于添加了
@ResponseBody
注释。它负责将响应对象正确序列化为 JSON。这部分在您的情况下抛出异常。