Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to retrieve information about spring mappings defined by @Controller?

I'd like to be able to create a page that lists off the mappings that were discovered by controllers annotated with @Controller. I'm doing this for REST services that allows us to easily find the endpoints available to the instance on a page.

So far I've manually been doing this, though if it's possible to create a controller that publishes this in a pleasant format nicely it would be greatly beneficial.

Key information I'm after is

  • Endpoint URI
  • Method (GET|POST|DELETE|...)
  • Params
  • Headers

Having access to this information where I could create a JSP view would be ideal.

like image 644
Brett Ryan Avatar asked Feb 22 '12 03:02

Brett Ryan


People also ask

What are the responsibilities of controller in spring?

In Spring MVC, controller methods are the final destination point that a web request can reach. After being invoked, the controller method starts to process the web request by interacting with the service layer to complete the work that needs to be done.

What does the most common controller configuration return to locate the view?

The controller returns an object of ModelAndView. The DispatcherServlet checks the entry of view resolver in the XML file and invokes the specified view component.

What needs to be done to identify a class by spring as a controller?

The @Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler.

How do I use a controller with spring boot?

@Controller annotation indicates that the annotated class is a controller. It is a specialization of @Component and is autodetected through classpath scanning. It is typically used in combination with annotated handler methods based on the @RequestMapping annotation.


2 Answers

With Spring 3.1, there is a new feature referred to as "end point documentation". The only thing I could find was some code in the spring-mvc-31-demo sample app. Refer to the example controller and JSP. Seems pretty straight forward.

like image 84
nickdos Avatar answered Nov 14 '22 01:11

nickdos


Check out this custom doclet https://github.com/rightshift/spring-mvc-api-doclet. It will create endpoint documentation for all Spring MVC @Controller annotated classes. Custom templates can be created to produce different HTML output. The new template name just needs to be passed as an optional param. It can also be added to as a reportSet to the reporting section of your maven pom.

like image 31
Barry Jordan Avatar answered Nov 14 '22 01:11

Barry Jordan