#1018 - HAL Forms properties customizable via annotations on representation model classes.
We now consider additional Jackson and JSR-303 annotations on representation models to enrich the HAL Forms template properties with additional information: - @NotNull on a property flips its `required` flag to `true`. - The "regexp" attribute of @Pattern on a property is forwarded into the "regex" field. - @JsonProperty(Access.READ_ONLY) on a property is translated into the "readOnly" flag. The default for the "required" flag have been changed to false even for PUT and POST as whether they're required or not is completely determined by the model, not the HTTP method. All of this is backed by significant refactoring in the way that Affordance instances are build internally. The new API is centered around the Affordances type in the mediatype package. The methods on Link to create the Affordance` from its details have been removed and replaced by builder style APIs on Affordance. AffordanceModelFactory has been moved to the mediatype as well. PropertyUtils has been significantly revamped to expose a PayloadMetadata/PropertyMetadata model to abstract the individual traits of a property. This allows to optionally plug in the support for JSR-303 annotations. AffordanceModelFactory has been refactored to rather work with those instead of ResolvableType.
This commit is contained in:
@@ -17,15 +17,15 @@ package org.springframework.hateoas.server.core;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.hateoas.Affordance;
|
||||
import org.springframework.hateoas.AffordanceModelFactory;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkRelation;
|
||||
import org.springframework.hateoas.QueryParameter;
|
||||
import org.springframework.hateoas.mediatype.AffordanceModelFactory;
|
||||
import org.springframework.hateoas.mediatype.Affordances;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@@ -60,15 +60,20 @@ public class SpringAffordanceBuilder {
|
||||
.orElse(ResolvableType.NONE);
|
||||
|
||||
List<QueryParameter> queryMethodParameters = parameters.getParametersWith(RequestParam.class).stream() //
|
||||
.map(it -> it.getParameterAnnotation(RequestParam.class)) //
|
||||
.filter(Objects::nonNull) //
|
||||
.map(it -> new QueryParameter(it.name(), it.value(), it.required())) //
|
||||
.map(QueryParameter::of) //
|
||||
.collect(Collectors.toList());
|
||||
|
||||
ResolvableType outputType = ResolvableType.forMethodReturnType(method);
|
||||
Affordances affordances = Affordances.of(affordanceLink);
|
||||
|
||||
return discoverer.getRequestMethod(type, method).stream() //
|
||||
.map(it -> new Affordance(methodName, affordanceLink, it, inputType, queryMethodParameters, outputType)) //
|
||||
.flatMap(it -> affordances.afford(it) //
|
||||
.withInput(inputType) //
|
||||
.withOutput(outputType) //
|
||||
.withParameters(queryMethodParameters) //
|
||||
.withName(methodName) //
|
||||
.build() //
|
||||
.stream()) //
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user