Skip to content

Commit

Permalink
Mention RegisterReflectionForBinding in the docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Oct 27, 2022
1 parent 65c68ba commit 0973a44
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
Expand Up @@ -130,21 +130,26 @@ The next time you build the native image, GraalVM will take these files into con
There are more advanced options which can be set on the native image tracing agent, for example filtering the recorded hints by caller classes, etc.
For further reading, please see https://www.graalvm.org/reference-manual/native-image/Agent/[the official documentation].



[[native-image.advanced.custom-hints]]
=== Custom Hints

If you need to provide your own hints for reflection, resources, serialization, proxy usage etc. you can use the `RuntimeHintsRegistrar` API.
Create a class that implements the `RuntimeHintsRegistrar` interface, then make appropriate calls to the provided `RuntimeHints` instance:

include::code:MyRuntimeHints[]

You can then use `@ImportRuntimeHints` on any `@Configuration` class (for example your `@SpringBootApplication` annotated application class) to activate those hints.

If you have classes which needs binding (mostly needed when serializing or deserializing JSON), you can use `@RegisterReflectionForBinding` on any bean.
Most of the hints are automatically inferred, for example when accepting or returning data from a `@RestController` method.
But when you work with `WebClient` or `RestTemplate` directly, you might need to use `RegisterReflectionForBinding`:

include::code:JsonSerialization[]


[[native-image.advanced.custom-hints.testing]]
==== Testing custom hints

The `RuntimeHintsPredicates` API can be used to test your hints.
The API provides methods that build a `Predicate` that can be used to test a `RuntimeHints` instance.

Expand Down
@@ -0,0 +1,44 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.docs.nativeimage.advanced.customhints;

import reactor.core.publisher.Mono;

import org.springframework.aot.hint.annotation.RegisterReflectionForBinding;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;

@Component
class JsonSerialization {

@RegisterReflectionForBinding(MyDto.class)
Mono<ResponseEntity<Void>> sendToServer(WebClient webClient) {
// MyDto is serialized to JSON
MyDto myDto = new MyDto();
return webClient.post().header("Content-Type", "application/json").bodyValue(myDto).retrieve()
.toBodilessEntity();
}

@RegisterReflectionForBinding(MyDto.class)
ResponseEntity<MyDto> retrieveFromServer(RestTemplate restTemplate) {
// MyDto is deserialized from JSON
return restTemplate.getForEntity("/my-dto", MyDto.class);
}

}
@@ -0,0 +1,25 @@
/*
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.docs.nativeimage.advanced.customhints;

class MyDto {

private String field;

// getter and setter

}

0 comments on commit 0973a44

Please sign in to comment.