Spring Data Rest-自定义Json模式/阿尔卑斯山? [英] Spring Data Rest - Custom Json Schema / Alps?

查看:111
本文介绍了Spring Data Rest-自定义Json模式/阿尔卑斯山?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将有关数据限制或默认值的信息提供给将使用该API的客户端应用程序. Spring Data Rest生成的模式或ALPS似乎是放置此信息的好地方.

My need is to give information about data constraints or default values to the client app that will use the API. The schema or the ALPS generated by Spring Data Rest seems to be a good place to put this information.

但是,在正式的参考文档中,关于API的文档介绍有些简短,在社区中找不到完整的示例文档.我试图阅读PersistentEntityToJsonSchemaConverter的代码以了解所提供的可能性,但首先出现了头痛.

But the part about documenting the API is a bit quick in the official reference documentation, and I can't find fully documented example in the community. I've tried to read the code of PersistentEntityToJsonSchemaConverter to have a insight of the offered possibilities, but the headache arrived first.

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

I know there is the @Description annotation that I can put on Entities and Properties that will change the title field of the schema. I know the same fields can be modified in rest-messages.properties

还有其他可由注释或配置文件修改的字段吗? 在此描述字段中放入默认或约束信息的感觉真的像是不直接使用它.

Is there other fields that can be modified by annotations or configuration files ? Putting default or constraints information in this description field really feels like not using it straight.

推荐答案

这个问题已经差不多了,我不知道您是否已经找到解决方案.

The question is almost old, I don't know if you have already found a solution.

在任何地方,如果您构建两个自定义转换器来替换Spring使用的转换器,则可以构建完全自定义的ALPS分析信息.

Anywhere, you can build a completely custom ALPS profiling information if you build two custom converters that replace the converters used by Spring.

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

The first one needs to extends the converter org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter.

这里是可能的实现方式:

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

}

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

The second one needs to extends the converter org.springframework.data.rest.webmvc.alps.RootResourceInformationToAlpsDescriptorConverter.

RootResourceInformationToAlpsDescriptorConverter只有两个公共资源:构造函数和转换"方法.

The RootResourceInformationToAlpsDescriptorConverter only have two public resources: the constructor and the "convert" method.

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

You may to overwrite every single private field/method of that class if you want to have a custom behaviour.

请注意,您的CustomAlpsJsonHttpMessageConverter支持" 方法将需要将给定的"converterType" 与新的CustomAlpsJsonHttpMessageConverter类进行匹配.

Pay attention that the "supports" method of your CustomAlpsJsonHttpMessageConverter will need to matches the given "converterType" with your new CustomAlpsJsonHttpMessageConverter class.

此时,您可以自定义类RootResourceInformationToAlpsDescriptorConverter"convert" 方法,只需将其覆盖在CustomRootResourceInformationToAlpsDescriptorConverter中即可.

At that point you can customize the "convert" method of the class RootResourceInformationToAlpsDescriptorConverter, simply ovverriding it in your CustomRootResourceInformationToAlpsDescriptorConverter.

最后,您必须在应用程序上下文中注册两个转换器.为此,您可以扩展RepositoryRestMvcConfiguration类,并在CustomRepositoryRestMvcConfiguration中需要@Override方法"alpsJsonHttpMessageConverter()""alpsConverter()".

Finally, you have to register the two converters in the Application Context. In order to do that, you can extend the RepositoryRestMvcConfiguration class, and in your CustomRepositoryRestMvcConfiguration you will need to @Override the methods "alpsJsonHttpMessageConverter()" and "alpsConverter()".

在两个ovverriding自定义方法中还添加@Bean批注,如下所示:

Add also the @Bean annotation in the two ovverriding custom methods, like this:

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

So you can have a completely custom ALPS, if you need.

我已经尝试过使用此解决方案来构建自定义配置文件链接,并且效果很好.

I have tried this solution to build custom profiling links, and it works perfectly.

这篇关于Spring Data Rest-自定义Json模式/阿尔卑斯山?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆