gpt4 book ai didi

java - Spring Boot 映射不起作用。 404状态

转载 作者:行者123 更新时间:2023-11-30 06:13:24 25 4
gpt4 key购买 nike

我正在逐步按照 youtube 上的教程进行操作,但没有得到结果。代码如下:

主要应用

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

问候 Controller

@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 的响应:

{
"timestamp": 1439128293727,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/api/greetings"
}

或来自 chrome 的以下内容:

Whitelabel Error Page

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

Sun Aug 09 08:43:41 COT 2015 There was an unexpected error (type=Not Found, status=404). No message available

我是跟着视频一步一步来的。视频中的导师似乎没有做任何额外的事情。每当我输入

localhost:8080/api/greetings

我得到的只是上述错误。

添加。这是控制台输出,我不知道它是否有帮助:

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 类的子包中,则不会扫描它,因此您将遇到此错误.

关于java - Spring Boot 映射不起作用。 404状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31905357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com