2015-10-28 22 views
5

Nhu cầu của tôi là cung cấp thông tin về ràng buộc dữ liệu hoặc giá trị mặc định cho ứng dụng khách sẽ sử dụng API. Giản đồ hoặc ALPS được tạo bởi Spring Data Rest có vẻ là một nơi tốt để đưa thông tin này.Nghỉ ngơi dữ liệu mùa xuân - Tùy chỉnh lược đồ Json/Alps?

Nhưng phần về cách ghi lại tài liệu API nhanh hơn một chút trong tài liệu tham chiếu chính thức và tôi không thể tìm thấy ví dụ được tài liệu đầy đủ trong cộng đồng. Tôi đã cố gắng đọc mã số PersistentEntityToJsonSchemaConverter để có một cái nhìn sâu sắc về khả năng được cung cấp, nhưng nhức đầu đến trước.

Tôi biết có chú thích @Description mà tôi có thể đặt trên Đối tượng và Thuộc tính sẽ thay đổi trường title của giản đồ. Tôi biết các trường giống nhau có thể được sửa đổi trong rest-messages.properties

Có các trường khác có thể được sửa đổi bằng chú thích hoặc tệp cấu hình không? Đặt thông tin mặc định hoặc ràng buộc trong trường mô tả này thực sự cảm thấy không sử dụng thẳng.

Trả lời

3

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()""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.

+0

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. –

Các vấn đề liên quan