Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Utility classes for implementing REST controllers for React Admin resources

License

NotificationsYou must be signed in to change notification settings

Amplicode/react-admin-utils

Repository files navigation

This library provides several useful utilities helping to implement REST controllers for React Admin-based frontend.

Compatibility

react-admin-utils works as a Spring Boot starter. Current version is compatible with Spring Boot3.2.x.

The library is a part of theAmplicode project, however it does not bring additional dependencies and can be used in any Spring Boot project.

The implementation heavily depends on Jackson (version used by the Spring Web).

Adding library to the project

The library has been published to the Maven Central repository.

Maven:

<dependency>    <groupId>io.amplicode</groupId>    <artifactId>react-admin-utils-starter</artifactId>    <version>0.2.0</version></dependency>

Gradle:

dependencies {    implementation 'io.amplicode:react-admin-utils-starter:0.2.0'}

ObjectPatcher

Patching

Theio.amplicode.rautils.patch.ObjectPatcher bean helps to implementPATCH REST endpoints.

The bean performs patching the passed object under the following set of conditions:

  • RealPATCH semantics is needed:
    • patch contains a subset of object attributes to modify
    • attributes not included into the patch are not modified
    • attributes can be reset to null
  • Data transfer object is animmutable class (POJO with all-args constructor, or a Java 17 record).

Usage:

importcom.fasterxml.jackson.databind.JsonNode;importio.amplicode.rautils.patch.ObjectPatcher;@AutowiredprivateObjectPatcherobjectPatcher;@PatchMapping("/{id}")publicResponseEntity<FooDto>patch(@PathVariableIntegerid,@RequestBodyJsonNodefooDtoPatch) {FooDtofooDto;// load current state of the entityFooDtopatchedDto =objectPatcher.patch(fooDto,fooDtoPatch);// save patchedDto to the data store    }

Note that if DTO used in the endpoint ismutable (e.g. has setters for all attributes), then an existing method from the Jackson library can be used instead of theObjectPatcher to patch the source object in-place:

importcom.fasterxml.jackson.databind.JsonNode;importcom.fasterxml.jackson.databind.ObjectMapper;@AutowiredprivateObjectMapperobjectMapper;@PatchMapping("/{id}")publicResponseEntity<FooDto>patch(@PathVariableIntegerid,@RequestBodyJsonNodefooDtoPatch)throwsIOException {FooDtofooDto;// load current state of the entityobjectMapper.readerForUpdating(fooDto).readValue(fooDtoPatch);// save fooDto to the data store    }

Validation

PATCH REST endpoints, unlike endpoints for other HTTP methods, cannot put@Valid on the request body argument, because the request body does not contain the full set of attributes that constitute the object to be validated.

Therefore, it is required to validate patched object in the endpoint code. Unfortunately, Spring doesn't provide one-liner API to perform such validation. As a shortcut to perform the validation,ObjectPatcher provides several methods:

importio.amplicode.rautils.patch.ObjectPatcher;@AutowiredprivateObjectPatcherobjectPatcher;// just validate// public void ObjectPatcher#validate(Object target);objectPatcher.validate(patchedDto);// patch and validate right away// public <T> T ObjectPatcher#patchAndValidate(T target, JsonNode patchJson)patchedDto =objectPatcher.patchAndValidate(sourceDto,patchJsonNode);// public <T> T ObjectPatcher#patchAndValidate(T target, String patchJson)patchedDto =objectPatcher.patchAndValidate(sourceDto,patchString);

In case of validation failure, these methods throw non-checked exceptionio.amplicode.rautils.patch.PatchValidationException that contains validation errors and is automatically processed by the Springorg.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver (resulting in400 Bad Request response code).

About

Utility classes for implementing REST controllers for React Admin resources

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp