Skip to content

Commit 8da1b6b

Browse files
committed
DATACMNS-1227 - Add documentation section about JSONPath- and XPath-based web data binding.
1 parent 34cadd0 commit 8da1b6b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/main/asciidoc/repositories.adoc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,43 @@ Assume we have 30 Person instances in the database. You can now trigger a reques
11941194

11951195
You see that the assembler produced the correct URI and also picks up the default configuration present to resolve the parameters into a `Pageable` for an upcoming request. This means, if you change that configuration, the links will automatically adhere to the change. By default the assembler points to the controller method it was invoked in but that can be customized by handing in a custom `Link` to be used as base to build the pagination links to overloads of the `PagedResourcesAssembler.toResource(…)` method.
11961196

1197+
[[core.web.binding]]
1198+
==== Web databinding support
1199+
1200+
Spring Data projections – generally described in <<projections>> – can be used to bind incoming request payloads by either using http://goessner.net/articles/JsonPath/[JSONPath] expressions (requires https://github.com/json-path/JsonPath[Jayway JasonPath] or https://www.w3.org/TR/xpath-31/[XPath] expressions (requires https://xmlbeam.org/[XmlBeam]).
1201+
1202+
.HTTP payload binding using JSONPath or XPath expressions
1203+
====
1204+
[source, java]
1205+
----
1206+
@ProjectedPayload
1207+
public interface UserPayload {
1208+
1209+
@XBRead("//firstname")
1210+
@JsonPath("$..firstname")
1211+
String getFirstname();
1212+
1213+
@XBRead("/lastname")
1214+
@JsonPath({ "$.lastname", "$.user.lastname" })
1215+
String getLastname();
1216+
}
1217+
----
1218+
====
1219+
1220+
The type above can be used as Spring MVC handler method argument or via `ParameterizedTypeReference` on one of ``RestTemplate``'s methods.
1221+
The method declarations above would try to find `firstname` anywhere in the given document.
1222+
The `lastname` XML looup is performed on the top-level of the incoming document.
1223+
The JSON variant of that tries a top-level `lastname` first but also tries `lastname` nested in a `user` sub-document in case the former doesn't return a value.
1224+
That way changes if the structure of the source document can be mitigated easily without having to touch clients calling the exposed methods (usually a drawback of class-based payload binding).
1225+
1226+
Nested projections are supported as described in <<projections>>.
1227+
If the method returns a complex, non-interface type, a Jackson `ObjectMapper` is used to map the final value.
1228+
1229+
For Spring MVC, the necessary converters are registered automatically, as soon as `@EnableSpringDataWebSupport` is active and the required dependencies are available on the classpath.
1230+
For usage with `RestTemplate` register a `ProjectingJackson2HttpMessageConverter` (JSON) or `XmlBeamHttpMessageConverter` manually.
1231+
1232+
For more information, see the https://github.com/spring-projects/spring-data-examples/tree/master/web/projection[web projection example] in the canonical https://github.com/spring-projects/spring-data-examples[Spring Data Examples repository].
1233+
11971234
[[core.web.type-safe]]
11981235
==== Querydsl web support
11991236

0 commit comments

Comments
 (0)