Skip to content

Commit c2b69d5

Browse files
committed
Merge pull request #23028 from bedla
* pr/23028: Polish "Add example of custom Actuator operations" Add example of custom Actuator operations Closes gh-23028
2 parents 947ba70 + f1455b1 commit c2b69d5

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,13 @@ If you add a `@Bean` annotated with `@Endpoint`, any methods annotated with `@Re
445445
Endpoints can be exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux.
446446
If both Jersey and Spring MVC are available, Spring MVC will be used.
447447

448+
The following example exposes a read operation that returns a custom object:
449+
450+
[source,java,indent=0]
451+
----
452+
include::{code-examples}/actuate/endpoint/CustomEndpointExample.java[tag=read]
453+
----
454+
448455
You can also write technology-specific endpoints by using `@JmxEndpoint` or `@WebEndpoint`.
449456
These endpoints are restricted to their respective technologies.
450457
For example, `@WebEndpoint` is exposed only over HTTP and not over JMX.
@@ -475,10 +482,15 @@ Consider the following JSON request body:
475482
}
476483
----
477484

478-
This can be used to invoke a write operation that takes `String name` and `int counter` parameters.
485+
This can be used to invoke a write operation that takes `String name` and `int counter` parameters, as shown in the following example:
486+
487+
[source,java,indent=0]
488+
----
489+
include::{code-examples}/actuate/endpoint/CustomEndpointExample.java[tag=write]
490+
----
479491

480492
TIP: Because endpoints are technology agnostic, only simple types can be specified in the method signature.
481-
In particular declaring a single parameter with a custom type defining a `name` and `counter` properties is not supported.
493+
In particular declaring a single parameter with a `CustomData` type defining a `name` and `counter` properties is not supported.
482494

483495
NOTE: To allow the input to be mapped to the operation method's parameters, Java code implementing an endpoint should be compiled with `-parameters`, and Kotlin code implementing an endpoint should be compiled with `-java-parameters`.
484496
This will happen automatically if you are using Spring Boot's Gradle plugin or if you are using Maven and `spring-boot-starter-parent`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuate.endpoint;
18+
19+
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
20+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
21+
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
22+
23+
/**
24+
* An example of a custom actuator endpoint.
25+
*
26+
* @author Stephane Nicoll
27+
*/
28+
@Endpoint(id = "custom")
29+
public class CustomEndpointExample {
30+
31+
// tag::read[]
32+
@ReadOperation
33+
public CustomData getCustomData() {
34+
return new CustomData("test", 5);
35+
}
36+
// end::read[]
37+
38+
// tag::write[]
39+
@WriteOperation
40+
public void updateCustomData(String name, int counter) {
41+
// injects "test" and 42
42+
}
43+
// end::write[]
44+
45+
public static class CustomData {
46+
47+
private final String name;
48+
49+
private final int counter;
50+
51+
public CustomData(String name, int counter) {
52+
this.name = name;
53+
this.counter = counter;
54+
}
55+
56+
}
57+
58+
}

0 commit comments

Comments
 (0)