Jason Lee Jason Lee - 10 days ago 4
Java Question

Simple Spring application returning error for route

I have created a simple Spring application with one route at "/a" but when I attempt to access that route, I get an error. The logs show that the route is being mapped, so I am not sure what is causing the error. There is no error message output in the logs.

HomeController.java

package com.12345;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HomeController {

static Logger log = Logger.getLogger(HomeController.class.getName());

@RequestMapping("/a")
public String home() {
log.debug("a route reached");
return "Home";
}
}


Application.java

package com.12345.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.12345")
@EnableConfigurationProperties
@EnableAutoConfiguration
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


Stacktrace and output

2016-11-22 10:36:40.512 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@59fd97a8: startup date [Tue Nov 22 10:36:35 PST 2016]; root of context hierarchy
2016-11-22 10:36:40.615 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/a]}" onto public java.lang.String com.12345.HomeController.home()
2016-11-22 10:36:40.620 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2016-11-22 10:36:40.620 INFO 644 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2016-11-22 10:36:40.665 INFO 644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-22 10:36:40.665 INFO 644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-22 10:36:40.717 INFO 644 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-11-22 10:36:41.045 INFO 644 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-11-22 10:36:41.137 INFO 644 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8085 (http)
2016-11-22 10:36:41.144 INFO 644 --- [ main] com.12345.config.Application : Started Application in 5.751 seconds (JVM running for 6.18)
2016-11-22 10:36:48.217 INFO 644 --- [nio-8085-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-11-22 10:36:48.217 INFO 644 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2016-11-22 10:36:48.242 INFO 644 --- [nio-8085-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 25 ms

===============
localhost:8085/a
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Nov 22 10:47:39 PST 2016
There was an unexpected error (type=Not Found, status=404).

No message available

Answer

I have always used the annotation:

@RestController
@RequestMapping("")

On the controller class for accessing the controller and always work.

How about you try:

package com.12345;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("")
public class HomeController {

  static Logger log = Logger.getLogger(HomeController.class.getName());

    @RequestMapping("/a")
    public String home() {
    log.debug("a route reached");
        return "Home";
    }
}

Then in the application I have:

@SpringBootApplication  

And that should make the trick:

package com.12345.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@ComponentScan(basePackages="com.12345")
@EnableConfigurationProperties
@EnableAutoConfiguration
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Comments