SpingBoot 映射不起作用.404状态 [英] SpingBoot Mapping not working. 404 status
问题描述
我正在逐步学习 youtube 上的教程,但没有得到结果.代码如下:
I am following a tutorial on youtube step by step but I am not getting a result. The code is as follows:
主要应用
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
模型类
public class Greeting {
private BigInteger id;
private String text;
public Greeting() {}
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
问候控制器
@RestController()
public class GreetingController {
private static BigInteger nextId;
private static Map<BigInteger, Greeting> greetingMap;
private static Greeting save(Greeting greeting) {
if (greetingMap == null) {
greetingMap = new HashMap<BigInteger, Greeting>();
nextId = BigInteger.ONE;
}
greeting.setId(nextId);
nextId = nextId.add(BigInteger.ONE);
greetingMap.put(greeting.getId(), greeting);
return greeting;
}
static {
Greeting g1 = new Greeting();
g1.setText("Hello World");
save(g1);
Greeting g2 = new Greeting();
g2.setText("Hola Mundo!");
save(g2);
}
@RequestMapping(
value = "/api/greetings",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Collection<Greeting>> getGreetings() {
Collection<Greeting> greetings = greetingMap.values();
return new ResponseEntity<Collection<Greeting>>(greetings, HttpStatus.OK);
}
}
Pom.xml 文件
<modelVersion>4.0.0</modelVersion>
<groupId>com.hakeem.webservice2</groupId>
<artifactId>WebService2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
每当我运行 get 请求时,我都会收到来自 Postman 的响应:
Whenever I run a get request, I get this response from Postman:
{
"timestamp": 1439128293727,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/greetings"
}
或来自 chrome 的以下内容:
or the following from chrome:
白标错误页面
此应用程序没有明确的/error 映射,因此您将其视为后备.
This application has no explicit mapping for /error, so you are seeing this as a fallback.
2015 年 8 月 9 日星期日 08:43:41 COT出现意外错误(类型=未找到,状态=404).没有可用的消息
Sun Aug 09 08:43:41 COT 2015 There was an unexpected error (type=Not Found, status=404). No message available
我一步一步地跟着视频.视频中的导师似乎没有做任何额外的事情.每当我输入
I followed the videos step by step. The tutor on the video has seemingly not done anything additional. Whenever I type in
本地主机:8080/api/greetings
localhost:8080/api/greetings
我得到的只是上述错误.
All I get is the aforementioned error.
补充.这是控制台输出,我不知道它是否有帮助:
Addition. This is the console output I do not know if it will be helpful:
2015-08-09 10:49:09.918 INFO 7696 --- [main] com.hakeem.webservice2.Application:在 PID 为 7696 的笔记本电脑上启动应用程序 (C:\Users\HakeemAbdussamad\Documents\MarsWorkspace\WebService2\目标\类由 HakeemAbdussamad 在 C:\Users\HakeemAbdussamad\Documents\MarsWorkspace\WebService2 中启动)2015-08-09 10:49:09.988 INFO 7696 --- [main] ationConfigEmbeddedWebApplicationContext : 刷新 org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bb0e64a: 启动日期 [Sun Aug 09:1090:T CO:4]上下文层次结构的根2015-08-09 10:49:10.710 INFO 7696 --- [ main] o.s.b.f.s.DefaultListableBeanFactory :覆盖 bean 'beanNameViewResolver' 的 bean 定义:替换 [Root bean: class [null];范围=;抽象=假;懒惰初始化=假;autowireMode=3;依赖检查=0;autowireCandidate=真;主要=假;factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration;factoryMethodName=beanNameViewResolver;initMethodName=null;destroyMethodName=(推断);定义在类路径资源 [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] 和 [Root bean: class [null];范围=;抽象=假;懒惰初始化=假;autowireMode=3;依赖检查=0;autowireCandidate=真;主要=假;factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter;factoryMethodName=beanNameViewResolver;initMethodName=null;destroyMethodName=(推断);在类路径资源中定义 [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]2015-08-09 10:49:11.547 INFO 7696 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat 初始化端口:8080 (http)2015-08-09 10:49:11.797 INFO 7696 --- [main] o.apache.catalina.core.StandardService:启动服务Tomcat2015-08-09 10:49:11.801 INFO 7696 --- [main] org.apache.catalina.core.StandardEngine:启动 Servlet 引擎:Apache Tomcat/8.0.152015-08-09 10:49:11.926 INFO 7696 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : 初始化 Spring 嵌入式 WebApplicationContext2015-08-09 10:49:11.926 INFO 7696 --- [ost-startStop-1] o.s.web.context.ContextLoader:根 WebApplicationContext:初始化在 1942 毫秒内完成2015-08-09 10:49:12.743 INFO 7696 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean:将 servlet:'dispatcherServlet' 映射到 [/]2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:映射过滤器:'characterEncodingFilter'到:[/]2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean:映射过滤器:'hiddenHttpMethodFilter'到:[/]2015-08-09 10:49:12.963 INFO 7696 --- [main] swsmmaRequestMappingHandlerAdapter :寻找@ControllerAdvice:org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bb0e64a:启动日期[8 月 10 日:49 年 8 月 10 日:09 COT 2015];上下文层次结构的根2015-08-09 10:49:13.035 INFO 7696 --- [main] swsmmaRequestMappingHandlerMapping:映射{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 到公共 org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2015-08-09 10:49:13.036 INFO 7696 --- [main] swsmmaRequestMappingHandlerMapping:映射{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" 到公共 org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)2015-08-09 10:49:13.108 INFO 7696 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping:映射 URL 路径 [/] 到类型 [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 的处理程序2015-08-09 10:49:13.108 INFO 7696 --- [main] oswshandler.SimpleUrlHandlerMapping:将 URL 路径 [/webjars/] 映射到 [class org.springframework.web.servlet.resource] 类型的处理程序上.ResourceHttpRequestHandler]2015-08-09 10:49:13.191 INFO 7696 --- [main] oswshandler.SimpleUrlHandlerMapping:将 URL 路径 [/**/favicon.ico] 映射到 [class org.springframework.web.servlet.resource 类型的处理程序上.ResourceHttpRequestHandler]2015-08-09 10:49:13.276 INFO 7696 --- [main] o.s.j.e.a.AnnotationMBeanExporter:在启动时为 JMX 公开注册 bean2015-08-09 10:49:13.329 INFO 7696 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer :Tomcat 在端口上启动:8080 (http)2015-08-09 10:49:13.331 INFO 7696 --- [main] com.hakeem.webservice2.Application:在 3.733 秒内启动应用程序(JVM 运行 4.32)
2015-08-09 10:49:09.918 INFO 7696 --- [ main] com.hakeem.webservice2.Application : Starting Application on Laptop with PID 7696 (C:\Users\HakeemAbdussamad\Documents\MarsWorkspace\WebService2\target\classes started by HakeemAbdussamad in C:\Users\HakeemAbdussamad\Documents\MarsWorkspace\WebService2) 2015-08-09 10:49:09.988 INFO 7696 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bb0e64a: startup date [Sun Aug 09 10:49:09 COT 2015]; root of context hierarchy 2015-08-09 10:49:10.710 INFO 7696 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]] 2015-08-09 10:49:11.547 INFO 7696 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2015-08-09 10:49:11.797 INFO 7696 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2015-08-09 10:49:11.801 INFO 7696 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.15 2015-08-09 10:49:11.926 INFO 7696 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2015-08-09 10:49:11.926 INFO 7696 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1942 ms 2015-08-09 10:49:12.743 INFO 7696 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/] 2015-08-09 10:49:12.747 INFO 7696 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/] 2015-08-09 10:49:12.963 INFO 7696 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@7bb0e64a: startup date [Sun Aug 09 10:49:09 COT 2015]; root of context hierarchy 2015-08-09 10:49:13.035 INFO 7696 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2015-08-09 10:49:13.036 INFO 7696 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest) 2015-08-09 10:49:13.108 INFO 7696 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-08-09 10:49:13.108 INFO 7696 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-08-09 10:49:13.191 INFO 7696 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2015-08-09 10:49:13.276 INFO 7696 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2015-08-09 10:49:13.329 INFO 7696 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2015-08-09 10:49:13.331 INFO 7696 --- [ main] com.hakeem.webservice2.Application : Started Application in 3.733 seconds (JVM running for 4.32)
推荐答案
给定的代码在我看来很正确.查看完整的源代码可能会有所帮助.例如 - 如果 GreetingController
类不在 Application
类的同一个包或子包中,它不会被扫描,所以你会出现这个错误.
The given code looks right to my eyes. Looking at the complete source may help. For example - if the GreetingController
class isn't in the same package or in a subpackage of the Application
class, it will not be scanned, and so you will have this error.
这篇关于SpingBoot 映射不起作用.404状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!