riz*_*joj 12 spring spring-data-jpa spring-data-rest
我有一个模特:
public class MyModel {
@Id private Long id;
private Long externalId;
// Getters, setters
}
Run Code Online (Sandbox Code Playgroud)
我想externalId用作我的资源标识符:
@Configuration
static class RepositoryEntityLookupConfig extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration configuration) {
configuration
.withEntityLookup()
.forRepository(MyRepository.class, MyModel::getExternalId, MyRepository::findByExternalId);
}
}
Run Code Online (Sandbox Code Playgroud)
如果externalId是String,这可以正常工作.但因为它是一个数字(Long)
public interface MyRepository extends JpaRepository<MyModel, Long> {
Optional<MyModel> findByExternalId(@Param("externalId") Long externalId);
}
Run Code Online (Sandbox Code Playgroud)
在调用时:/myModels/1我得到:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at org.springframework.data.rest.core.config.EntityLookupConfiguration$RepositoriesEntityLookup.lookupEntity(EntityLookupConfiguration.java:213) ~[spring-data-rest-core-2.6.4.RELEASE.jar:na]
at org.springframework.data.rest.core.support.UnwrappingRepositoryInvokerFactory$UnwrappingRepositoryInvoker.invokeFindOne(UnwrappingRepositoryInvokerFactory.java:130) ~[spring-data-rest-core-2.6.4.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:524) ~[spring-data-rest-webmvc-2.6.4.RELEASE.jar:na]
at org.springframework.data.rest.webmvc.RepositoryEntityController.getItemResource(RepositoryEntityController.java:335) ~[spring-data-rest-webmvc-2.6.4.RELEASE.jar:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
...
Run Code Online (Sandbox Code Playgroud)
单独的自定义EntityLookupSupport<MyModel>组件类可以工作.
我是否遗漏了一些东西让Long我在我的工作中使用方法参考RepositoryRestConfigurerAdapter?
尝试将其添加到您的RepositoryEntityLookupConfig班级中:
@Override
public void configureConversionService(ConfigurableConversionService conversionService) {
conversionService.addConverter(String.class, Long.class, Long::parseLong);
super.configureConversionService(conversionService);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
827 次 |
| 最近记录: |