Spring Data Rest Json / Alps user schema?

I need to provide information about data restrictions or default values ​​for a client application that will use the API. The schema or ALPS generated by Spring Data Rest seems to be a good place to host this information.

But part of the API documentation has accelerated a bit in the official reference documentation, and I cannot find a fully documented example in the community. I tried reading the PersistentEntityToJsonSchemaConverter code to get an idea of ​​the features offered, but the headache came first.

I know that there is an @Description annotation that I can impose on objects and properties that will change the title field of the schema. I know that the same fields can be changed in rest-messages.properties

Are there other fields that can be modified by annotations or configuration files? Putting default data or restrictions in this description field really looks like you are not using it directly.

+7
source share
1 answer

The question is almost old, I do not know if you have found a solution.

Anywhere, you can create fully custom ALPS profiling information if you create two custom converters that replace the converters used by Spring.

The first should org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter converter org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter .

Here is a possible implementation:

 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); } } 

The second is org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter converter org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter .

RootResourceInformationToAlpsDescriptorConverter has only two public resources: the constructor and the convert method.

You can overwrite each private field / method of this class if you want to have your own behavior.

Note that the " CustomAlpsJsonHttpMessageConverter " CustomAlpsJsonHttpMessageConverter your CustomAlpsJsonHttpMessageConverter will have to match the specified "converterType" with your new CustomAlpsJsonHttpMessageConverter class.

At this point, you can customize the "convert" method of the RootResourceInformationToAlpsDescriptorConverter class by simply overriding it in your CustomRootResourceInformationToAlpsDescriptorConverter .

Finally, you must register the two converters in the application context. To do this, you can extend the RepositoryRestMvcConfiguration class, and in your CustomRepositoryRestMvcConfiguration you will need the @Override methods "alpsJsonHttpMessageConverter()" and "alpsConverter()" .

Also add the @Bean annotation in two custom ovverriding methods, for example:

 @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()); } 

This way you can have fully custom ALPS if you need to.

I tried this solution to create custom profiling links and it works great.

+4
source

Source: https://habr.com/ru/post/1234719/


All Articles