首页 > 解决方案 > 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).

标签: spring-bootspring-dataspring-data-restspringdoc

解决方案


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:

enter image description here


推荐阅读