在自定义控制器中接受Spring Data REST URI [英] Accepting a Spring Data REST URI in custom controller
问题描述
我有一个Spring Data Rest webmvc应用程序,我想为批处理操作添加一些自定义功能。
I have a Spring Data Rest webmvc application that I'd like to add some custom functionality to for batch operations.
我已经创建了一个控制器,并且混合了它进入uri命名空间,但我希望它能够像自定义 / search
查询一样接受URI,而不仅仅是ID。
I've created a controller, and blended it into the uri namespace, but I'd like for it to be able to accept URI's like the custom /search
queries do, rather than simply an ID.
我已经尝试注册自定义< String,Long>
转换器(我的实体有一个 Long
ID类型,但似乎被忽略。有没有办法配置我的控制器,使其从自动实现的SDR控制器采用该行为?
I have tried registering a custom <String, Long>
converter (my entity has a Long
ID type, but that seems to get ignored. Is there any way to configure my controller such that it adopts that behavior from the auto-implemented SDR controllers?
即使我可以调用某种方法来自动解析实体的URI,这也可以正常工作(因为我可以简单地接受 String
在我的控制器中)
Even if there is some sort of method I can call that will auto-resolve a URI to an entity, that would work just as well (as I can then simply accept a String
in my controller)
这就是我在的地方。
@Configuration
public class CustomWebConfiguration extends WebMvcConfigurationSupport {
//irrelevant code omitted
@Bean
public DomainClassConverter<?> domainClassConverter() {
DomainClassConverter<FormattingConversionService> dc = new DomainClassConverter<FormattingConversionService>(mvcConversionService());
return dc;
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(String.class, Long.class, testConverter());
}
@Bean
Converter<String, Long> testConverter() {
return new Converter<String, Long>() {
@Override
public Long convert(String source) {
//this code does _not_ get run at any point
if (source.indexOf('/') == -1) { return Long.parseLong(source); }
source = source.substring(source.lastIndexOf('/') + 1);
Long id = Long.parseLong(source);
return id;
}
};
}
}
SDR配置
@Configuration
@EnableHypermediaSupport(type = { HypermediaType.HAL })
public class CustomRestConfiguration extends RepositoryRestMvcConfiguration {
@Override
public RepositoryRestConfiguration config() {
RepositoryRestConfiguration config = super.config();
config.setBasePath("/api");
config.exposeIdsFor(ApplicationMembership.class);
return config;
}
}
我的(设计)控制器:
ApplicationType是我的实体之一,由SDR /存储库魔法正确管理
ApplicationType is one of my entities that are correctly managed by SDR/repository magic
@BasePathAwareController
@RepositoryRestController
@RequestMapping("applications/special")
public class ApplicationExtensionController {
@RequestMapping("a")
public ResponseEntity<?> reply(@RequestParam("type") ApplicationType type) {
return new ResponseEntity<String>(type.getIcon(), HttpStatus.OK);
}
}
我看了很多但是可以'完全可以使任何事都有效。当我创建一个利用存储库的< String,ApplicationType>
转换器时,它也不会被调用,因为DomainClassConverter只调用它的底层< ;字符串,长>
转换器(显然失败了,因为它无法正确地将 types / 1
解析为long。
I've looked around quite a bit but can't quite manage to make anything work. When I create a <String, ApplicationType>
converter that utilizes the repository, it also does not get called, as the DomainClassConverter just calls its underlying <String, Long>
converter (which obviously fails, as it cannot correctly parse out types/1
into a long.
感谢帮助!
忘记提及
- Spring Data Rest 2.4.0
- Spring HATEOAS 0.19.0
- Spring 4.2.1
使用JPA存储库
推荐答案
事实证明,我当时在添加转换器的正确轨道,不幸的是我用错误的配置方法。
As it turns out, I was on the right track with adding a converter, unfortunately I was doing it in the wrong configuration method.
通过移动我的我能够获得所需的功能testConverter()
bean到 RepositoryRestMvcConfiguration
扩展配置类,然后添加
I was able to get the desired functionality by moving my testConverter()
bean to the RepositoryRestMvcConfiguration
extension config class and then adding
@Override
public void configureConversionService(ConfigurableConversionService service) {
service.addConverter(testConverter());
}
按预期工作。我现在感觉有点傻,因为首先把它扔在错误的地方,但希望这会帮助别人出去!
And working as intended. I feel a bit silly now for throwing that in the wrong spot in the first place, but hopefully this will help someone else out!
这篇关于在自定义控制器中接受Spring Data REST URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!