问题是当将 Spring 缓存与 Redis 缓存管理器一起使用时,由于没有默认构造函数,无法反序列化 Spring Pageable 响应
使用的spring boot版本是2.1.4.RELEASE
使用序列化器的 Redis 配置类
@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Spring 缓存和 Redis 作为缓存后端在 Redis 缓存中缓存 Spring REST API 页面结果响应
@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
Run Code Online (Sandbox Code Playgroud)
我可以Page<Article>使用序列化程序在 Redis 缓存中看到以 JSON 形式缓存,RedisSerializer.json()但在下一次调用期间,当从缓存中读取数据时,出现以下异常
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of `org.springframework.data.domain.PageImpl` (no
Creators, like default construct, exist): cannot deserialize from Object
value (no delegate- or …Run Code Online (Sandbox Code Playgroud) fasterxml spring-boot spring-cache spring-data-commons jackson2
我正在尝试使用PagingAndSortingRepository带有find MyEntity where field in fieldValues查询的spring ,如下所示:
@Repository
public interface MyEntity extends PagingAndSortingRepository<MyEntity, String> {
List<MyEntity> findByMyField(Set<String> myField);
}
Run Code Online (Sandbox Code Playgroud)
但没有成功.
我希望上面的函数返回其字段与其中一个字段值匹配的所有实体,但它只返回空结果.
即使它看起来像一个非常直接的能力,我在文档中找不到任何引用.
是/如何实现?
谢谢.
java spring spring-data spring-data-mongodb spring-data-commons
此问题出现在Spring-Data发行版2中.在最新版本1.13.9(及更早版本)中,它运行正常.
控制器代码:
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
@RequestMapping(value = "sorttest", method = RequestMethod.GET)
public Page<Integer> getDummy() {
return new PageImpl<>(Collections.singletonList(1), new PageRequest(0, 5, new Sort("asdf")), 1);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring-Data 2风格相同:
Pageable pageable = PageRequest.of(0, 10, new Sort(Sort.Direction.ASC, "asd"));
PageImpl<Integer> page = new PageImpl<Integer>(Lists.newArrayList(1,2,3), pageable, 3);
return page;
Run Code Online (Sandbox Code Playgroud)
组态:
@SpringBootApplication
@EnableWebMvc
@EnableSpringDataWebSupport
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
还尝试了简单的Spring应用程序,没有带有Java配置的Spring Boot以及XML配置.结果是一样的:
{
"content": …Run Code Online (Sandbox Code Playgroud) 是什么区别getSize(),并getNumberOfElements在Spring数据类org.springframework.data.domain.Slice?
Javadoc在这里没有提供太多帮助.
我在我的REST控制器中使用了spring-data-common的PagedResourcesAssembler,我很高兴看到它甚至在响应中生成了下一个/上一个链接.但是,在我有其他查询参数(除了页面,大小,排序)的情况下,这些参数不包含在生成的链接中.我能以某种方式配置汇编程序以包含链接中的参数吗?
非常感谢,丹尼尔
我已经使用WebMvcConfigurerAdapter已有一段时间了。由于无法使用getInterceptors()方法获取所有已注册的拦截器,因此我切换到了WebMvcConfigurationSupport,它具有许多默认的已注册Spring Bean,例如ContentNegotiationManager,ExceptionHandlerExceptionResolverusw。
现在,我已经意识到,尽管我在WebConfig类上使用了@EnableSpringDataWebSupport批注,但非常方便的DomainClassConverter(使用CrudRepository将域类ID转换为域类对象)并未默认注册。
当我像这样显式定义此bean时,它就会起作用。
@EnableSpringDataWebSupport
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Bean
public DomainClassConverter<?> domainClassConverter() {
return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
}
}
Run Code Online (Sandbox Code Playgroud)
但是,为什么EnableSpringDataWebSupport无法与WebMvcConfigurationSupport一起使用?
JavaLocalDate实现了Comparable<ChronoLocalDate>,但它应该已经实现Comparable<LocalDate>以允许创建Range<LocalDate>. 之所以如此,是因为声明是Range<T extends Comparable<T>>:
spring-data-commons/src/main/java/org/springframework/data/domain/Range.java
问题是是否可以spring-data-commons Range有以下类声明:
public class Range<T extends Comparable<? super T>> {
Run Code Online (Sandbox Code Playgroud)
这可能有助于创建Range<LocalDate>.
ID getId()其中一个接口是“org.springframework.data.domain.Persistable”,它是一个java接口,具有第3方lib中的方法。
另一个接口是 Kotlin 接口interface IdEntry { val id: String}。
现在我的业务入口需要实现这两个接口:
data class MyEntry(
override val id: String,
....// more properties
) : IdEntry, Persistable<String>
Run Code Online (Sandbox Code Playgroud)
我使用IntelliJ IDE编码,错误是:
Class 'MyEntry' is not abstract and does not implement abstract member
@Nullable public abstract fun getId(): String!
defined in org.springframework.data.domain.Persistable
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题?
我还尝试了下面的代码:(来自这里的想法)
data class MyEntry(
private val id: String,
....// more properties
) : IdEntry, Persistable<String> {
override fun getId() = id
...
}
Run Code Online (Sandbox Code Playgroud)
但这也失败了:
Cannot weaken access …Run Code Online (Sandbox Code Playgroud) 很难弄清楚我做错了什么。遗憾的是我曾经让它工作过,但无法确定我改变了什么破坏了它。
据我了解,现在应该完全支持。
有问题的对象:
@Document
public class Place {
public final static String URI = "/place";
@Id private String id;
private String name;
private String caption;
private GeoJsonPoint location;
public Place() {}
public Place(GeoJsonPoint geoJsonPoint) {
this.location = geoJsonPoint;
}
// Proper getters & setters clipped.
}
Run Code Online (Sandbox Code Playgroud)
调用(出于某种原因,我的 Spring Boot 版本包含额外的 x/y 坐标。)
{
"id": null,
"name": null,
"caption": null,
"location": {
"x": 41.988161,
"y": -87.6911499,
"type": "Point",
"coordinates": [
41.988161,
-87.6911499
]
}
}
Run Code Online (Sandbox Code Playgroud)
Pom(也许我有错误/冲突的依赖关系?)
<?xml version="1.0" encoding="UTF-8"?> …Run Code Online (Sandbox Code Playgroud) geojson spring-data-rest spring-data-mongodb spring-data-commons
spring-data ×5
spring ×4
java ×3
fasterxml ×1
geojson ×1
jackson2 ×1
kotlin ×1
range ×1
spring-boot ×1
spring-cache ×1
spring-mvc ×1