Câu hỏi là gần như cũ, tôi không biết nếu bạn đã tìm thấy một giải pháp.
Mọi nơi, bạn có thể tạo thông tin lược tả ALPS hoàn toàn tùy chỉnh nếu bạn xây dựng hai trình biến đổi tùy chỉnh thay thế trình biến đổi được Spring sử dụng.
Đầu tiên cần kéo dài công cụ chuyển đổi org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter
.
Dưới đây là một thực thể:
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) {
if (converterType.equals(AlpsJsonHttpMessageConverter.class))
return true;
else if (converterType.equals(CustomAlpsJsonHttpMessageConverter.class))
return true;
else return false;
}
}
Điều thứ hai cần phải mở rộng bộ chuyển đổi org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter
.
RootResourceInformationToAlpsDescriptorConverter
chỉ có hai tài nguyên công khai: hàm tạo và phương thức "chuyển đổi".
Bạn có thể ghi đè mọi trường/phương thức riêng tư của lớp đó nếu bạn muốn có hành vi tùy chỉnh.
chú ý Pay rằng "hỗ trợ" phương pháp CustomAlpsJsonHttpMessageConverter
bạn sẽ cần phải phù hợp với trao "converterType" với lớp CustomAlpsJsonHttpMessageConverter
mới của bạn.
Tại thời điểm mà bạn có thể tùy chỉnh "chuyển đổi" phương pháp của lớp RootResourceInformationToAlpsDescriptorConverter
, chỉ cần ovverriding nó trong CustomRootResourceInformationToAlpsDescriptorConverter
của bạn.
Cuối cùng, bạn phải đăng ký hai bộ chuyển đổi trong Ngữ cảnh ứng dụng. Để thực hiện điều đó, bạn có thể mở rộng lớp RepositoryRestMvcConfiguration
và trong số CustomRepositoryRestMvcConfiguration
bạn cần phải các phương pháp "alpsJsonHttpMessageConverter()"
và "alpsConverter()"
.
Add cũng là @Bean
chú thích trong hai phương pháp tùy chỉnh ovverriding, như thế này:
@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());
}
Vì vậy, bạn có thể có một ALPS hoàn toàn tùy chỉnh, nếu bạn cần.
Tôi đã thử giải pháp này để tạo liên kết lược tả tùy chỉnh và hoạt động hoàn hảo.
cảm ơn bạn! Tôi không làm việc trên dự án nữa, và chúng tôi đã kết thúc việc có một tài liệu bên ngoài cho điều đó, cắt bỏ "vẻ đẹp" của API tự giải thích Dù sao, tôi sẽ liên kết câu trả lời của bạn với các đồng đội cũ để họ có một cái nhìn sâu sắc về những việc cần làm để quay lại tài liệu rõ ràng hơn. –