Spring Data Rest - 自定义Json Schema / Alps?

时间:2015-10-28 17:40:03

标签: java rest spring-data spring-data-rest jsonschema

我需要向将使用API​​的客户端应用程序提供有关数据约束或默认值的信息。 Spring Data Rest生成的架构或ALPS似乎是放置此信息的好地方。

但是关于记录API的部分在官方参考文档中有点快,我无法在社区中找到完整记录的示例。我已经尝试阅读PersistentEntityToJsonSchemaConverter的代码,以便了解所提供的可能性,但首先是头疼。

我知道我可以在实体和属性上添加@Description注释,它将更改架构的title字段。 我知道可以在rest-messages.properties

中修改相同的字段

是否有其他字段可以通过注释或配置文件进行修改? 在这个描述字段中放置默认或约束信息真的感觉就像不直接使用它。

1 个答案:

答案 0 :(得分:4)

这个问题几乎已经过时了,我不知道你是否已找到解决方案。

如果您构建两个替换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) {
        if (converterType.equals(AlpsJsonHttpMessageConverter.class))
            return true;
        else if (converterType.equals(CustomAlpsJsonHttpMessageConverter.class))
            return true;
        else return false;
    }

}

第二个需要扩展转换器org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter

RootResourceInformationToAlpsDescriptorConverter只有两个公共资源:构造函数和&#34;转换&#34;方法

如果您想要自定义行为,则可以覆盖该类的每个私有字段/方法。

请注意CustomAlpsJsonHttpMessageConverter&#34;支持&#34; 方法需要与给定的&#34; converterType&#34; 使用新的CustomAlpsJsonHttpMessageConverter课程。

此时,您可以自定义类RootResourceInformationToAlpsDescriptorConverter&#34;转换&#34; 方法,只需将其归入CustomRootResourceInformationToAlpsDescriptorConverter

最后,您必须在应用程序上下文中注册两个转换器。为此,您可以扩展RepositoryRestMvcConfiguration课程,在CustomRepositoryRestMvcConfiguration @Override中,您需要"alpsJsonHttpMessageConverter()"方法"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()); } 注释,如下所示:

QList<Plugin *> get_plugins()
  {
    static QList<Plugin*> list {new PluginA() }; 
    return list;
  }

如果需要,您可以拥有完全自定义的ALPS。

我已经尝试过这个解决方案来构建自定义配置文件链接,它运行得很好。