View Javadoc
1   package org.springframework.boot.actuate.endpoint;
2   
3   import static java.util.stream.Collectors.joining;
4   import static org.apache.commons.lang3.StringUtils.isNotBlank;
5   
6   import com.google.common.collect.Lists;
7   
8   import org.springframework.beans.factory.annotation.Autowired;
9   import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
10  import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
11  import org.springframework.context.ApplicationContext;
12  import org.springframework.core.annotation.AnnotationUtils;
13  import org.springframework.http.ResponseEntity;
14  
15  import java.util.List;
16  import java.util.Map;
17  import java.util.Map.Entry;
18  
19  @Endpoint(id = "show-all")
20  public class ShowAllEndpoint {
21  
22      private ApplicationContext applicationContext;
23  
24      public ShowAllEndpoint(final ApplicationContext applicationContext) {
25          this.applicationContext = applicationContext;
26      }
27  
28      @ReadOperation
29      public ResponseEntity getEndpoints() {
30          final Map<String, Object> endpointBeans = this.applicationContext.getBeansWithAnnotation(Endpoint.class);
31  
32          final List<String> endpoints = Lists.newLinkedList();
33          for (final Entry<String, Object> entry : endpointBeans.entrySet()) {
34              final Object endpointInstance = entry.getValue();
35              final Class endpointClazz = endpointInstance.getClass();
36              final Endpoint annotation = AnnotationUtils.findAnnotation(endpointClazz, Endpoint.class);
37              final String id = "" + AnnotationUtils.getValue(annotation, "id");
38              endpoints.add(isNotBlank(id) ? id : "'" + entry.getKey() + "'");
39          }
40          endpoints.sort(String::compareTo);
41  
42          return ResponseEntity.ok().body(endpoints.stream().collect(joining(",\n")));
43      }
44  
45      @Autowired(required = false)
46      public void setObjectMappers(final List<com.fasterxml.jackson.databind.ObjectMapper> objectMappers) {
47          objectMappers.forEach(objectMapper ->
48              objectMapper.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
49          );
50      }
51  }