spring-boot - Disable setting up Audit fields in create/update requests in Spring Data REST
问题描述
I'm using combination of various Spring components - Boot (2.3), Data, Data REST, Springdoc. In my model objects I use auditing - I annotate some fields with @CreatedBy, @CreatedDate etc. I would like to disable possibility to set value of those audit fields through REST API. At the same time, I want this information to be available when retrieving data.
Seems like quite obvious thing to do, but I'm unable to find a way to do this. By default I can easily provide those values in API calls and see them persisted.
Ideally, such configuration change would be visible also in OpenAPI spec generated by Springdoc (in model of request).
解决方案
So it turns out that I'm silly :)
So my error was that authentication and authorization was disabled at that time. Once enabled, I wasn't able to provide values for createdBy
and other fields as they were just getting overridden with correct values.
When it comes to OpenAPI specification, I had to annotate fields with:
@Schema(accessMode = Schema.AccessMode.READ_ONLY)
from io.swagger.v3.oas.annotations.media.Schema;
. This resulted in correct info. See Swagger view:
推荐阅读
- javascript - 如何将 Fabric js 与 React Native 集成?
- android - 自 Chrome 72 以来,WebView 无法解析 SVG
- python - 使用 Selenium/Python 处理“无标题”页面
- reactjs - Redux 一个接一个地同步调度多个动作
- c++ - OPC-UA:由(自发行)CA 签名的证书返回 NULL 而没有错误消息,我错过了什么?
- spring - Spring Security 从 OAuth2AuthenticationToken 获取 tokenValue
- typo3 - 如何将内联元素添加到 flexforms 中并使其工作
- apache - httpd-vhosts.conf 中的重定向无法在 XAMPP 中按预期工作
- python - 如何比较两列数据以确保没有数据匹配
- java - 如何在 Java 中从数组中添加或删除随机数?