发生意外错误(类型=未找到,状态= 404)。 -春季Java

时间:2020-05-11 19:08:31

标签: java spring

我在github https://github.com/felloz/personsService中提供了此服务,应用程序编译没有错误,但是当我尝试访问路由http://localhost:8080/demo/persons时,我收到此错误:

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

Mon May 11 14:55:30 VET 2020
There was an unexpected error (type=Not Found, status=404).
No message available

编译消息:

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.7.RELEASE)

2020-05-11 15:05:55.576  INFO 10476 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on lserrano-PC with PID 10476 (C:\JAVA\Eclipse Projects\demo\target\classes started by lserrano in C:\JAVA\Eclipse Projects\demo)
2020-05-11 15:05:55.592  INFO 10476 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-05-11 15:05:58.173  INFO 10476 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-11 15:05:58.198  INFO 10476 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-11 15:05:58.198  INFO 10476 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-05-11 15:05:58.386  INFO 10476 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-11 15:05:58.387  INFO 10476 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2685 ms
2020-05-11 15:05:58.857  INFO 10476 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-05-11 15:05:59.362  INFO 10476 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-11 15:05:59.377  INFO 10476 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 4.578 seconds (JVM running for 5.433)
2020-05-11 15:06:14.712  INFO 10476 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-11 15:06:14.712  INFO 10476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-05-11 15:06:14.727  INFO 10476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 15 ms

您可以在github存储库中检查代码,即使用NetBeans作为IDE,Sprong的新功能将不胜感激。

添加server.servlet.context-path = / demo

之后

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.7.RELEASE)

2020-05-11 17:26:54.336  INFO 9144 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on lserrano-PC with PID 9144 (C:\JAVA\Eclipse Projects\demo\target\classes started by lserrano in C:\JAVA\Eclipse Projects\demo)
2020-05-11 17:26:54.336  INFO 9144 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-05-11 17:26:56.926  INFO 9144 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-11 17:26:56.957  INFO 9144 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-11 17:26:56.957  INFO 9144 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.34]
2020-05-11 17:26:57.160  INFO 9144 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/demo]   : Initializing Spring embedded WebApplicationContext
2020-05-11 17:26:57.160  INFO 9144 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2715 ms
2020-05-11 17:26:57.612  INFO 9144 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-05-11 17:26:58.111  INFO 9144 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path '/demo'
2020-05-11 17:26:58.111  INFO 9144 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 4.591 seconds (JVM running for 5.464)
2020-05-11 17:27:14.361  INFO 9144 --- [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/demo]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-11 17:27:14.362  INFO 9144 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-05-11 17:27:14.379  INFO 9144 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 17 ms

3 个答案:

答案 0 :(得分:1)

您的控制器中有@RequestMapping({"/persons"}),因此请尝试使用不http://localhost:8080/persons的链接demo

答案 1 :(得分:1)

在春季2之后,上下文路径配置已更改;

server.servlet.context-path=/demo

然后尝试http://localhost:8080/demo/persons

答案 2 :(得分:1)

如果您不想使用Jpa,则需要进行以下更改:

Context Path应该设置为:

server.servlet.context-path=/demo

DemoApplication类(更正基本包):

@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {

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

}

您需要为Repositoory提供实施:

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PersonRepository{
    public List<Person> findAll(){
        List<Person> list = new ArrayList<>();
        Person person = new Person();
        person.setId(10001);
        person.setName("Name");
        person.setLastname("LastName");
        list.add(person);
        return list; 
    }
}

要使用MySQL DB和SpringBoot,可以在Web上找到许多示例。请遵循。范例:SpringBootMySQLExample