[英]Why do I always get Whitelabel Error Page with status "404" while running a simple Spring Boot Application
[英]Spring application starts but I always see the “Whitelabel Error Page”
我有一个Spring应用程序,分为两个模块。 我有一个包含实体,存储库和服务的“核心”模块,还有一个我想创建一些Rest Controller来处理该数据的“ Web”模块。 我将向您展示到目前为止的模块结构和文件:
核心模块:
|-- core
|-- model
|-- client
|-- Client.java
|-- repositories
|-- client
|-- IClientRepository.java
|-- services
|-- client
|-- IClientService.java
|-- ClientServiceImpl.java
Client.java:
package model;
import javax.persistence.*;
@Entity
@Table(name="clients")
public class Client {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID")
private Integer id;
@Column(name="NAME")
private String name;
@Column(name="EMAIL")
private String email;
@Column(name="PHONE")
private String phone;
public Client () {
}
public Client (String name, String email, String phone) {
this.email = email;
this.name = name;
this.phone = phone;
}
@Override
public String toString () {
return "[" + id + "] " + name + " (" + email + ", " + phone + ")";
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
IClientRepository.java:
package repositories.client;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import model.Client;
import java.util.List;
import java.util.Optional;
@Repository
public interface IClientRepository extends CrudRepository<Client, Integer> {
List<Client> findAll ();
Optional<Client> findById (Integer id);
Client save(Client client);
void deleteById (Integer ID);
}
IClientService.java:
package services.client;
import model.Client;
import java.util.List;
import java.util.Optional;
public interface IClientService {
List<Client> findAll ();
Optional<Client> findById (Integer id);
void deleteById (Integer id);
Client save (Client client);
}
ClientServiceImpl.java:
package services.client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Service;
import model.Client;
import repositories.client.IClientRepository;
import java.util.List;
import java.util.Optional;
@Service
public class ClientServiceImpl implements IClientService{
@Autowired
private IClientRepository repository;
@Override
public List<Client> findAll() {
return repository.findAll();
}
@Override
public Optional<Client> findById(Integer id) {
return repository.findById(id);
}
@Override
public void deleteById(Integer id) {
repository.deleteById(id);
}
@Override
public Client save(Client client) {
return repository.save(client);
}
}
Web模块:
|-- web
|-- rest
|-- client
|-- ClientController.java
|-- main
|-- Application.java
ClientController.java:
package rest.client;
import model.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import services.client.IClientService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
@RestController
public class ClientController {
@Autowired
private IClientService clientService;
@RequestMapping("/clients")
public List<Client> getAllClients() {
return clientService.findAll();
}
}
Application.java:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication(scanBasePackages = {"rest"})
@ComponentScan({"services"})
@EntityScan("model")
@EnableJpaRepositories("repositories")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我使用xampp随附的MySql数据库,并且我知道核心模块有效,因为我在具有简单控制台界面的另一个项目中使用了它。 另外,IntelliJ在每个模块中都创建一个“ resources”文件夹,其中有一个application.properties文件,其中包含以下内容:
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://localhost:3306/movierental
spring.datasource.username=root
现在,核心模块可以独立工作,如果我只是简单地返回带有客户端的静态创建的列表,那么其余控制器也可以工作,但是当我将它们一起使用以便将数据从数据库获取到剩余控制器时,其余控制器就无法工作。 我转到“ localhost:8080 / clients”,我得到了Whitelabel错误页面,我也不知道为什么。 我在这里做错什么了吗?
编辑:
访问链接后,这就是我在控制台中看到的内容:
2018-05-31 20:09:48.128 INFO 13804 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-05-31 20:09:48.128 INFO 13804 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-05-31 20:09:48.147 INFO 13804 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms
这是出现在“白色标签”页面上的内容:
白标错误页面
此应用程序没有针对/ error的显式映射,因此您将其视为后备。
2018年5月31日星期四20:09:48
发生意外错误(类型=未找到,状态= 404)。
无讯息
@SpringBootApplication
注释等效于使用@Configuration
@EnableAutoConfiguration
, @ComponentScan
@EnableAutoConfiguration
和@ComponentScan
及其默认属性。
这是有关如何替代默认行为的示例代码。
@SpringBootApplication(scanBasePackages = "com.example")
或者,您可以指定多个软件包,如下所示:
@SpringBootApplication(scanBasePackages = {"com.example", "com.example2"})
Spring Boot默认应用程序结构和组件扫描
事实证明,按照惯例,如果您的主应用程序类带有@SpringBootApplication
批注,并且位于您的所有代码位于此位置以下的包中,则默认情况下将扫描这些子包。
package example.simplespringboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
默认情况下,示例中example.simplespringboot下面的所有软件包都将被扫描以查找Spring组件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.