1

Is there a way to tell if Spring has loaded my @Controller?

I'm requesting a URL but I'm not hitting my controller and I can't figure out why

I'm loading controllers by doing a component scan

<context:component-scan base-package="com.example.app.web"/>

Other controllers in the same package as my failing controller are working fine.

My controller code is:

@Controller
@RequestMapping(value = "/app/administration/ecosystem")
public class AppEcosystemController {

    @Autowired
    EcosystemManagerService ecosystemManagerService;

    @RequestMapping(value = "/Foo", method = RequestMethod.GET)
    public String getEcosystem() {
        /* Implementation */
    }

The first thing I'd like to do is to be sure that this controller is getting picked up by the component scan.

Any suggestions?

davioooh
  • 23,742
  • 39
  • 159
  • 250
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • Are you sure your comonents are being correctly scanned/registered in application context? Is this problem happening only for this controller or also for others? – davioooh Jul 30 '14 at 07:30
  • Just this one controller; all others in the package are getting picked up – Dancrumb Jul 30 '14 at 13:53
  • Resolved the loading issue by telling IntelliJ to clean and rebuild... *shrug* – Dancrumb Jul 30 '14 at 14:18

3 Answers3

4

Just enable logging for your application, you can find this information at INFO level
For example in my application I have a controller named UserController.

The following log4j.properties does the trick.

log4j.rootLogger=INFO, FILE
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=../logs/rest-json.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

I can see in the log that RequestMappingHandlerMapping mapped my controller (scroll all the way to the right).

07:28:36,255  INFO RequestMappingHandlerMapping:182 - Mapped "{[/rest/**/users/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[text/xml || application/json],custom=[]}" onto public org..domain.User org.ramanh.controller.UserController.getUser(java.lang.String)
07:28:36,255  INFO RequestMappingHandlerMapping:182 - Mapped "{[/rest/**/users],methods=[POST],params=[],headers=[],consumes=[],produces=[text/xml || application/json],custom=[]}" onto public void org..controller.UserController.addUser(org...domain.User)

If you are still unsure I would suggest adding a method annotated with @PostConstruct. You could easily look up the message in the log or place a break point in this method.

@PostConstruct
protected void iamAlive(){
    log.info(“Hello AppEcosystemController”)
}

If you find that your controller is initialized correctly but still the url is not accessible.I would test the following

  1. You are getting 404 error - maybe you are not pointing to the correct url (do not forget to add the application as prefix to the url)
  2. You are getting 404 error - Dispatcher servlet mapping in web.xml doesn't meet the url above
  3. You are getting 403/401 – maybe you are using spring security and it’s blocking the url
  4. You are getting 406 – your content type definition is conflicting with your request
  5. You are getting 50x – something is buggy in your code
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
1

I made an ApplicationContextDumper. Add it into application context, it will dump all beans and their dependencies in the current context and parent contexts (if any) into log file when the application context initialization finishes. It also lists the beans which aren’t referenced.

It was inspired by this answer.

Community
  • 1
  • 1
aleung
  • 9,848
  • 3
  • 55
  • 69
0

You could start out with enabling debug logging for Spring as outlined here.

I'd also recommend leveraging the MVC testing support, which you'll find in the spring-test jar. Details on how to use it can be found here and here.

Community
  • 1
  • 1
wmkoch
  • 175
  • 9