这个问题已经很老了,不知道你是否已经找到解决方案了。
在任何地方,如果您构建两个自定义转换器来替换 Spring 使用的转换器,则可以构建完全自定义的 ALPS 分析信息。
第一个需要扩展转换器org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter
.
这是一个可能的实现:
public class CustomAlpsJsonHttpMessageConverter extends AlpsJsonHttpMessageConverter {
public CustomAlpsJsonHttpMessageConverter(RootResourceInformationToAlpsDescriptorConverter converter) {
super(converter);
}
@Override
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
return super.canWrite(clazz, mediaType);
}
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
return super.canRead(type, contextClass, mediaType);
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request,
ServerHttpResponse response) {
return super.beforeBodyWrite(body, returnType, selectedContentType, selectedConverterType, request, response);
}
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return converterType.equals(AlpsJsonHttpMessageConverter.class)
|| converterType.equals(CustomAlpsJsonHttpMessageConverter.class);
}
}
第二个需要扩展转换器org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter
.
The RootResourceInformationToAlpsDescriptorConverter
只有两个公共资源:构造函数和“convert”方法。
如果您想要自定义行为,您可以覆盖该类的每个私有字段/方法。
请注意“支持”你的方法CustomAlpsJsonHttpMessageConverter
将需要匹配给定的“转换器类型”与你的新CustomAlpsJsonHttpMessageConverter
class.
此时您可以自定义“转变”类的方法RootResourceInformationToAlpsDescriptorConverter
,只需在您的CustomRootResourceInformationToAlpsDescriptorConverter
.
最后,您必须在应用程序上下文中注册这两个转换器。为此,您可以扩展RepositoryRestMvcConfiguration
类,并且在你的CustomRepositoryRestMvcConfiguration
你需要@Override
方法"alpsJsonHttpMessageConverter()"
and "alpsConverter()"
.
还添加@Bean
在两个 ovverriding 自定义方法中添加注释,如下所示:
@Bean
@Override
public AlpsJsonHttpMessageConverter alpsJsonHttpMessageConverter() {
return new CustomAlpsJsonHttpMessageConverter(alpsConverter());
}
@Bean
@Override
public RootResourceInformationToAlpsDescriptorConverter alpsConverter() {
Repositories repositories = repositories();
PersistentEntities persistentEntities = persistentEntities();
RepositoryEntityLinks entityLinks = entityLinks();
MessageSourceAccessor messageSourceAccessor = resourceDescriptionMessageSourceAccessor();
RepositoryRestConfiguration config = config();
ResourceMappings resourceMappings = resourceMappings();
return new CustomRootResourceInformationToAlpsDescriptorConverter(associationLinks(), repositories, persistentEntities,
entityLinks, messageSourceAccessor, config, objectMapper(), enumTranslator());
}
因此,如果需要,您可以拥有完全定制的 ALPS。
我已经尝试过此解决方案来构建自定义分析链接,并且效果非常好。