我创建了一个简单的Spring应用程序,在“/a”处有一个路由,但是当我尝试访问该路由时,我得到了一个错误。日志显示正在Map该路由,所以我不确定是什么导致了这个错误。日志中没有错误消息输出。
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);
}
}
堆栈跟踪和输出
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
5条答案
按热度按时间nnsrf1az1#
我一直用这样的注解:
关于控制器类,用于访问控制器并始终工作.
不如你试试:
然后在应用程序中,我有:
这应该能说明问题:
zvokhttg2#
将
@ResponseBody
添加到home()
应该可以解决这个问题,如果没有它,返回“Home”意味着响应静态文件Home
。或者,将
@Controller
更改为@RestController
也可以。@RestController
等于@Controller
+@ResponseBody
。oxalkeyp3#
此处:
这里返回一个名为"Home"的视图,所以必须有一个Home.
ext
,其中ext
是用于视图解析器的后缀扩展名。如果添加
@ResponseBody
,则返回String "Home"作为响应的内容,这是完全不同的。wgxvkvu94#
使用您提供的代码,请求实际上被接收。如果您没有配置您的日志记录器从调试类别实际记录,您的日志语句可能已经消失。
这里的错误是home()方法的返回值。它将导致Spring使用您的视图解析器,或者如果您没有配置视图解析器,它将返回到默认值InternalResourceViewResolver。它将查找以下文件“src/main/resources/static/Home”并将其作为视图转发。由于在您的案例中没有找到它,Spring将尝试转发到未定义的错误页面。
mwngjboj5#
请使用servletPath.getRoutePath()检查你的路由路径是否确实是http://localhost:8080.然后继续你的Map(“/a”).这将起作用.