2

I want to list all registered request mappings in the log at application startup (Spring MVC, log4j). Ideally I want to view two things for each entry: URL and corresponding controller's method.

What have I tried: I searched in corresponding documentation entry without success.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Maksym Demidas
  • 7,707
  • 1
  • 29
  • 36

3 Answers3

1

Please check this blogpost for more information about how to log Spring MVC mappings in your application, source blogpost.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

@Controller
public class EndpointDocController {

    private final RequestMappingHandlerMapping handlerMapping;

    @Autowired
    public EndpointDocController(RequestMappingHandlerMapping handlerMapping) {

        this.handlerMapping = handlerMapping;

    }

    @RequestMapping(value = "/endpointdoc", method = RequestMethod.GET)
    public void show(Model model) {

        model.addAttribute("handlerMethods",
                this.handlerMapping.getHandlerMethods());

    }
}
erhun
  • 3,549
  • 2
  • 35
  • 44
1

From spring's code (org.springframework.web.servlet.handler.AbstractHandlerMethodMapping):

this.handlerMethods.put(mapping, newHandlerMethod);                        
if (logger.isInfoEnabled()) {                                              
    logger.info("Mapped \"" + mapping + "\" onto " + newHandlerMethod);    
}                                                                          

As you see the code is already there and mapped at INFO level (apache-commons-logging), so you should see every @RequestMapping by default, no matter if they are in @Controller or @RestController.


Because you ask for it, first make sure that a logger-implementation is available. If you see

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

At application start there is no logger-implementation! If this message occour, you have to decide what logger-implementation you like to use. The commonly most used are slf4j, log4j and jul.

Slf4j / Maven

Add this to the dependencies to see your output.

<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version> </dependency>

Why not use commons-logging?

Simply its a api to be used by others, not an implementation, not a single System.out is made by commons-logging.

Grim
  • 1,938
  • 10
  • 56
  • 123
0

Looks like starting from Spring 3.1 you need to activate info logging for org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(...).

Maksym Demidas
  • 7,707
  • 1
  • 29
  • 36