1

In the past when working with Spring REST in a JVM as the application would bootup, just before the container would completely boot, the Spring MVC module would output a list of all the RESTful API's that it was configured to expose? I've since been unable to reproduce this in later versions of spring? I realise that doing so is a security risk if anyone can get a hold of your logs but I need a list of the REST calls exposed by our system as quickly as possible. If anyone has any suggestions I'd greatly appreciate it.

Thanks,
Mark.

Mark Gargan
  • 831
  • 1
  • 9
  • 21
  • Probably you want a list for "REST services consumers" that don't have and don't want to have access to the source code. In this case, you can use http://mvnrepository.com/artifact/org.codehaus.enunciate/maven-enunciate-plugin . You just add some annotations + comments and it will generate an HTML file (+additional resources) as a documentation page. This can be useful: http://stackoverflow.com/questions/11939542/how-do-you-document-a-rest-api – ROMANIA_engineer Sep 24 '15 at 10:48

2 Answers2

3

I am not sure if it is exactly what you are looking for - we came up with an IndexController that renders a response containing all the Controllers that are annotated with ExposesResourceFor in HATEOAS style. So we get entry points to the collection resource for every controller.

You could do something similar with your controllers annotated with RestController or RequestMapping.

@RestController
@RequestMapping(path = "/", produces = { "application/hal+json", "application/json" })
public class IndexController {
    private final Set<Class<?>> entitiesWithController;
    private EntityLinks entityLinks;
    private RelProvider relProvider;

    @Autowired
    public IndexController(ListableBeanFactory beanFactory, EntityLinks entityLinks, RelProvider relProvider) {
        this.entityLinks = entityLinks;
        this.relProvider = relProvider;
        Map<String, Object> beansWithExposesResourceForAnnotation = beanFactory.getBeansWithAnnotation(ExposesResourceFor.class);
        entitiesWithController = beansWithExposesResourceForAnnotation.values().stream()
                .map(o -> o.getClass().getAnnotation(ExposesResourceFor.class).value()).collect(Collectors.toSet());
    }

    @RequestMapping(method = GET)
    public ResponseEntity<ControllerLinksResource> getControllerLinks() {
        ControllerLinksResource controllerLinksResource = new ControllerLinksResource();

        entitiesWithController.forEach(entityClass -> controllerLinksResource //
                .add(entityLinks.linkToCollectionResource(entityClass) //
                        .withRel(relProvider.getCollectionResourceRelFor(entityClass))));

        return ResponseEntity.ok(controllerLinksResource);
    }
}
Mathias Dpunkt
  • 11,594
  • 4
  • 45
  • 70
  • Thanks a million for that Zaddo, that appears to give you back every Controller annotated class in the jvm? Alas I'm looking for a list of all the exposed URI's. I've definitely seen this printed immediately before spring completes wiring and the tomcat server starts in the catalina.log. I'm really wondering how it is I turn it back on. – Mark Gargan Sep 24 '15 at 12:55
  • 1
    Hey - if it helped you could still upvote my anwer ;-) - The code above searches for controllers annotated with ExposesResourceFor - but you could change it to `@RestController` or `@RequestMapping`. Do you mean such log entries? - `Mapped "{[/system/metrics/{name:.*}],methods=[GET]}" onto public java.lang.Object ...` - I see them when my spring boot application is starting - the output comes from `org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping` – Mathias Dpunkt Sep 24 '15 at 13:04
  • Bingo that's exactly what I'm looking for Mathias!! Apologies now I didn't realise I hadn't upvoted your answer! how do I flag this reply as the answer? – Mark Gargan Sep 24 '15 at 13:22
  • 1
    Hi There, I am unable to compile the code snippet. Can you pls post what you and how used. – Sam May 18 '16 at 20:38
  • should indicate spring's versions, because in 2019 this code does not compile, even though I am sure it is a great solution. – OhadR Dec 15 '19 at 14:43
0

Basically you need REST API documentation, You can use any of the below Rest API documentation frameworks.

Swagger

apiDoc

JsonDoc

Lovababu Padala
  • 2,415
  • 2
  • 20
  • 28
  • Thanks a lot Lovababu alas that would still mean trawling through each controller and updating them manually. It's definitely something I'll incorporate in the future but for right now I need a way of finding the list without having to touch each controller. we've lots :D – Mark Gargan Sep 24 '15 at 12:52