简单的Spring应用程序返回路由错误

时间:2016-11-22 19:15:37

标签: java spring spring-boot

我创建了一个简单的Spring应用程序,在" / a"但是当我尝试访问该路线时,我收到错误。日志显示路由正在映射,因此我不确定导致错误的原因。日志中没有输出错误消息。

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和输出

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

4 个答案:

答案 0 :(得分:2)

我一直使用注释:

@RestController
@RequestMapping("")

在控制器类上访问控制器并始终工作。

你怎么试试:

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";
    }
}

然后在申请表中我有:

@SpringBootApplication  

这应该成功:

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);
    }
}

答案 1 :(得分:1)

@ResponseBody添加到home()应解决此问题。 没有它,返回&#34; Home&#34;表示响应静态文件Home

或者,将@Controller更改为@RestController也有效。 @RestController@Controller + @ResponseBody

答案 2 :(得分:0)

这里:

*.asm

这里返回一个名为“Home”的视图。因此,您必须拥有一个主页。@RequestMapping("/a") public String home() { log.debug("a route reached"); return "Home"; } 其中ext是用于查看解析程序的后缀扩展名。
如果添加ext,则返回字符串“Home”作为响应的内容。这是完全不同的。

答案 3 :(得分:0)

使用您提供的代码,实际收到请求。如果您尚未将记录器配置为从调试类别实际记录,则您的日志语句可能已消失。

这里的错误是home()方法的返回值。它会导致Spring使用您的视图解析器,或者如果您还没有配置它,它将回退到默认的InternalResourceViewResolver。它将查找以下文件&#34; src / main / resources / static / Home&#34;并以视图的形式转发。由于在您的情况下找不到,Spring将尝试转发到未定义的错误页面。