我在Spring Boot 2.1.0.RELEAS中使用spring-boot-starter-data-rest和spring-boot-starter-data-cassandra。
当我对具有复合主键的实体执行GET时,出现以下错误: org.springframework.core.convert.ConverterNotFoundException:未找到能够从[java.lang.String]类型转换为[com .... RankByYearAndNameKey]类型的转换器
下面是我的代码:
@PrimaryKeyClass
public class RankByYearAndNameKey implements Serializable {
@PrimaryKeyColumn(name="race_year", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private Integer raceYear;
@PrimaryKeyColumn(name="race_name", ordinal = 1, type = PrimaryKeyType.PARTITIONED)
private String raceName;
@PrimaryKeyColumn(name="rank", ordinal = 2, type = PrimaryKeyType.CLUSTERED)
private Integer rank;
public Integer getRaceYear() {
return raceYear;
}
...
}
@Table("rank_by_year_and_name")
public class RankByYearAndName {
@PrimaryKey
private RankByYearAndNameKey key;
@Column("cyclist_name")
private String cyclistName;
public RankByYearAndNameKey getKey() {
return key;
}
public void setKey(RankByYearAndNameKey key) {
this.key = key;
}
public String getCyclistName() {
return cyclistName;
}
public void setCyclistName(String cyclistName) {
this.cyclistName = cyclistName;
}
...
}
public class RankByYearAndNameKeyReadConverter implements Converter<String, RankByYearAndNameKey> {
public RankByYearAndNameKey convert(String source) {
if (StringUtils.hasText(source)) {
try {
return new ObjectMapper().readValue(source, RankByYearAndNameKey.class);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
return null;
}
}
public class RankByYearAndNameKeyWriteConverter implements Converter<RankByYearAndNameKey, String> {
public String convert(RankByYearAndNameKey source) {
try {
return new ObjectMapper().writeValueAsString(source);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
和配置文件:
@Configuration
@EnableCassandraRepositories(basePackages = { "com.demo.cassandradatarest.repo" })
public class ApplicationConfig extends AbstractClusterConfiguration
{
.....
@Bean
public CassandraCustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<Converter<?, ?>>();
converters.add(new RankByYearAndNameKeyReadConverter());
converters.add(new RankByYearAndNameKeyWriteConverter());
CassandraCustomConversions customConversions = new CassandraCustomConversions(converters);
return customConversions;
}
@Bean
public CassandraMappingContext mappingContext() throws ClassNotFoundException {
CassandraMappingContext mappingContext = new CassandraMappingContext();
mappingContext.setInitialEntitySet(getInitialEntitySet());
CassandraCustomConversions customConversions = customConversions();
mappingContext.setCustomConversions(customConversions);
//mappingContext.setSimpleTypeHolder(customConversions.getSimpleTypeHolder());
mappingContext.setUserTypeResolver(new SimpleUserTypeResolver(cluster().getObject(), getKeyspaceName()));
return mappingContext;
}
@Bean
public CassandraConverter converter() throws ClassNotFoundException {
MappingCassandraConverter mappingCassandraConverter = new MappingCassandraConverter(mappingContext());
mappingCassandraConverter.setCustomConversions(customConversions());
return mappingCassandraConverter;
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(getKeyspaceName());
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
....
}
当我执行 http://localhost:8181/rankByYearAndNames
{
"_embedded": {
"rankByYearAndNames": [
{
"cyclistName": "Daniel MARTIN",
"_links": {
"self": {
"href": "http://localhost:8181/rankByYearAndNames/com.demo.cassandradatarest.entity.key.RankByYearAndNameKey@798fb380"
},
"rankByYearAndName": {
"href": "http://localhost:8181/rankByYearAndNames/com.demo.cassandradatarest.entity.key.RankByYearAndNameKey@798fb380"
}
}
},
当我在自我链接上执行GET时:“ http://localhost:8181/rankByYearAndNames/com.demo.cassandradatarest.entity.key.RankByYearAndNameKey@798fb380”
发生以下异常: org.springframework.core.convert.ConverterNotFoundException:未找到能够从[java.lang.String]类型转换为[com .... RankByYearAndNameKey]类型的转换器