Package de.odrotbohm.spring.web.model
Class MappedPayloads.MappedPayload<T>
java.lang.Object
de.odrotbohm.spring.web.model.MappedPayloads.MappedErrors
de.odrotbohm.spring.web.model.MappedPayloads.MappedPayload<T>
- Enclosing interface:
- MappedPayloads
A monadic type to work with a mapped payload alongside
Errors to build up pipelines to eventually result in
an HttpEntity based on the processing steps.- Author:
- Oliver Drotbohm
-
Field Summary
Fields inherited from class de.odrotbohm.spring.web.model.MappedPayloads.MappedErrors
onErrors -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.HttpEntity<?>concludeIfValid(BiFunction<? super T, org.springframework.validation.Errors, ? extends org.springframework.http.ResponseEntity<?>> finalizer) Concludes the processing of the current payload with the given finalizer, but automatically produces an error response via the callbacks registered foronErrors(java.util.function.Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>>)andonAbsence.<S,R extends org.springframework.http.ResponseEntity<? extends S>>
RconcludeIfValid(Function<? super T, R> finalizer) Concludes the processing of the current payload with the given finalizer, but automatically produces an error response via the callbacks registered foronErrors(java.util.function.Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>>)andonAbsence.org.springframework.http.HttpEntity<?>concludeSelfIfValid(BiFunction<? super T, MappedPayloads.MappedErrors, ? extends org.springframework.http.ResponseEntity<?>> finalizer) Concludes the processing of the current payload with the given finalizer, but automatically produces an error response via the callbacks registered foronErrors(java.util.function.Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>>)andonAbsence.org.springframework.http.HttpEntity<?>Terminal operation that produces an empty response withHttpStatus.NO_CONTENT.protected Optional<org.springframework.http.ResponseEntity<?>><S> MappedPayloads.MappedPayload<S><S> MappedPayloads.MappedPayload<S>flatMapIfValid(Function<? super T, Optional<S>> mapper) <S> MappedPayloads.MappedPayload<S>map(BiFunction<? super T, org.springframework.validation.Errors, S> mapper) <S> MappedPayloads.MappedPayload<S>Applies the givenFunction, even if errors have been accumulated.<S> MappedPayloads.MappedPayload<S>mapIfValid(BiFunction<? super T, org.springframework.validation.Errors, S> mapper) <S> MappedPayloads.MappedPayload<S>mapIfValid(Function<? super T, S> mapper) Applies the givenFunctionif no errors have been accumulated or we're on a path to aHttpStatus.NOT_FOUNDyet.notFoundIf(boolean guard) Switches theMappedPayloads.MappedPayloadto unconditionally produce anHttpEntitywith status codeHttpStatus.NOT_FOUND.notFoundIf(Predicate<? super T> predicate) Turns theMappedPayloads.MappedPayloadto unconditionally produce anHttpEntitywith status codeHttpStatus.NOT_FOUNDif the given predicate on the payload matches.Registers aFunctionto create anResponseEntityin case the pipeline yields the absence of a payload.onAbsenceReject(String field, String errorCode) Registers the given field to be rejected on payload absence.onErrors(Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>> callback) peek(BiConsumer<? super T, org.springframework.validation.Errors> consumer) peekIfValid(BiConsumer<? super T, org.springframework.validation.Errors> consumer) Peeks at the given payload andErrorsif no errors have been accumulated or we're on a path to aHttpStatus.NOT_FOUNDyet.peekIfValid(Consumer<? super T> consumer) Peeks at the payload if present and no errors have been accumulated or we're on a path to aHttpStatus.NOT_FOUNDyet.rejectField(boolean condition, String field, String errorCode) Rejects the field with the given name with the given error code if the given condition is true.rejectField(boolean condition, String field, String errorCode, Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>> errorHandler) Rejects the field with the given name with the given error code if the given condition is true.rejectField(String field, String errorCode) Rejects the field of the given name with the given error code.rejectField(String field, String errorCode, String defaultMessage) Rejects the field with the given name with the given error code and default message.rejectField(Predicate<T> condition, String field, String errorCode) Rejects the field with the given name with the given error code if the given condition predicate returns true.validate(BiConsumer<? super T, org.springframework.validation.Errors> validator) Syntactic sugar to invoke validations in a readable way.Methods inherited from class de.odrotbohm.spring.web.model.MappedPayloads.MappedErrors
onValidGet, peekErrors, toBadRequest, with
-
Method Details
-
notFoundIf
Switches theMappedPayloads.MappedPayloadto unconditionally produce anHttpEntitywith status codeHttpStatus.NOT_FOUND. Customize the eventual creation of that usingonAbsence(Supplier).- Parameters:
guard- whether to end up withHttpStatus.NOT_FOUNDor not.- Returns:
- will never be null.
- See Also:
-
notFoundIf
Turns theMappedPayloads.MappedPayloadto unconditionally produce anHttpEntitywith status codeHttpStatus.NOT_FOUNDif the given predicate on the payload matches. Customize the eventual creation of that usingonAbsence(Supplier).- Parameters:
predicate- must not be null.- Returns:
- will never be null.
- See Also:
-
peekIfValid
Peeks at the payload if present and no errors have been accumulated or we're on a path to aHttpStatus.NOT_FOUNDyet.- Parameters:
consumer- must not be null.- Returns:
- the current instance, never null.
-
peekIfValid
public MappedPayloads.MappedPayload<T> peekIfValid(BiConsumer<? super T, org.springframework.validation.Errors> consumer) Peeks at the given payload andErrorsif no errors have been accumulated or we're on a path to aHttpStatus.NOT_FOUNDyet.- Parameters:
consumer-- Returns:
- the current instance, never null.
-
peek
-
peek
public MappedPayloads.MappedPayload<T> peek(BiConsumer<? super T, org.springframework.validation.Errors> consumer) -
validate
public MappedPayloads.MappedPayload<T> validate(BiConsumer<? super T, org.springframework.validation.Errors> validator) Syntactic sugar to invoke validations in a readable way. Essentially the same aspeek(BiConsumer). Easy to use with Spring's ownValidatorinterface or the YAVIYaviValidatoradapter.- Parameters:
validator- must not be null.- Returns:
-
map
Applies the givenFunction, even if errors have been accumulated.- Type Parameters:
S-- Parameters:
mapper- must not be null.- Returns:
- will never be null.
-
flatMap
-
mapIfValid
Applies the givenFunctionif no errors have been accumulated or we're on a path to aHttpStatus.NOT_FOUNDyet.- Type Parameters:
S-- Parameters:
mapper- must not be null.- Returns:
-
flatMapIfValid
-
map
public <S> MappedPayloads.MappedPayload<S> map(BiFunction<? super T, org.springframework.validation.Errors, S> mapper) -
mapIfValid
public <S> MappedPayloads.MappedPayload<S> mapIfValid(BiFunction<? super T, org.springframework.validation.Errors, S> mapper) -
onErrors
public MappedPayloads.MappedPayload<T> onErrors(Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>> callback) Description copied from class:MappedPayloads.MappedErrorsRegisters aFunctionto eventually turn anErrorsinstance into anHttpEntity. Will only be used if theErrorshave accumulated at least one error in the pipeline.- Overrides:
onErrorsin classMappedPayloads.MappedErrors- Parameters:
callback- must not be null.- Returns:
-
onErrors
public MappedPayloads.MappedPayload<T> onErrors(Supplier<org.springframework.http.ResponseEntity<?>> callback) - Overrides:
onErrorsin classMappedPayloads.MappedErrors
-
onAbsence
public MappedPayloads.MappedPayload<T> onAbsence(Supplier<org.springframework.http.ResponseEntity<?>> callback) Registers aFunctionto create anResponseEntityin case the pipeline yields the absence of a payload.- Parameters:
callback- must not be null.- Returns:
- See Also:
-
onAbsenceReject
Registers the given field to be rejected on payload absence. Transparently registers the error handler as absence handler.- Parameters:
field- must not be null or empty.errorCode- must not be null or empty.- Returns:
- will never be null.
-
concludeIfValid
public <S,R extends org.springframework.http.ResponseEntity<? extends S>> R concludeIfValid(Function<? super T, R> finalizer) Concludes the processing of the current payload with the given finalizer, but automatically produces an error response via the callbacks registered foronErrors(java.util.function.Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>>)andonAbsence.- Parameters:
finalizer- must not be null.- Returns:
-
concludeIfValid
public org.springframework.http.HttpEntity<?> concludeIfValid(BiFunction<? super T, org.springframework.validation.Errors, ? extends org.springframework.http.ResponseEntity<?>> finalizer) Concludes the processing of the current payload with the given finalizer, but automatically produces an error response via the callbacks registered foronErrors(java.util.function.Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>>)andonAbsence.- Parameters:
finalizer- must not be null.- Returns:
-
concludeSelfIfValid
public org.springframework.http.HttpEntity<?> concludeSelfIfValid(BiFunction<? super T, MappedPayloads.MappedErrors, ? extends org.springframework.http.ResponseEntity<?>> finalizer) Concludes the processing of the current payload with the given finalizer, but automatically produces an error response via the callbacks registered foronErrors(java.util.function.Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>>)andonAbsence.- Parameters:
finalizer- must not be null.- Returns:
-
concludeWithoutContent
public org.springframework.http.HttpEntity<?> concludeWithoutContent()Terminal operation that produces an empty response withHttpStatus.NO_CONTENT.- Returns:
-
rejectField
public MappedPayloads.MappedPayload<T> rejectField(boolean condition, String field, String errorCode) Description copied from class:MappedPayloads.MappedErrorsRejects the field with the given name with the given error code if the given condition is true.- Overrides:
rejectFieldin classMappedPayloads.MappedErrors- Parameters:
condition- the condition under which to reject the given field.field- must not be null or empty.errorCode- must not be null or empty.- Returns:
- the current instance, never null.
-
rejectField
public MappedPayloads.MappedPayload<T> rejectField(Predicate<T> condition, String field, String errorCode) Rejects the field with the given name with the given error code if the given condition predicate returns true.- Parameters:
condition- the condition predicate under which to reject the given field.field- must not be null or empty.errorCode- must not be null or empty.- Returns:
- the current instance, never null.
-
rejectField
Description copied from class:MappedPayloads.MappedErrorsRejects the field of the given name with the given error code.- Overrides:
rejectFieldin classMappedPayloads.MappedErrors- Parameters:
field- must not be null or empty.errorCode- must not be null or empty.- Returns:
- the current instance, never null.
-
rejectField
public MappedPayloads.MappedPayload<T> rejectField(String field, String errorCode, String defaultMessage) Description copied from class:MappedPayloads.MappedErrorsRejects the field with the given name with the given error code and default message.- Overrides:
rejectFieldin classMappedPayloads.MappedErrors- Parameters:
field- must not be null or empty.errorCode- must not be null or empty.defaultMessage- must not be null or empty.- Returns:
- the current instance, never null.
-
rejectField
public MappedPayloads.MappedPayload<T> rejectField(boolean condition, String field, String errorCode, Function<org.springframework.validation.Errors, org.springframework.http.ResponseEntity<?>> errorHandler) Rejects the field with the given name with the given error code if the given condition is true.- Overrides:
rejectFieldin classMappedPayloads.MappedErrors- Parameters:
condition- the condition under which to reject the given field.field- must not be null or empty.errorCode- must not be null or empty.errorHandler- and error handler to be registered in case the condition is true.- Returns:
- the current instance, never null.
-
errorsOrNone
- Overrides:
errorsOrNonein classMappedPayloads.MappedErrors
-