Spring应用程序启动但我总是看到“Whitelabel错误页面”

时间:2018-05-31 16:32:26

标签: java spring

我将Spring应用程序拆分为两个模块。一个'核心'模块,我有我的实体,存储库和服务以及一个'web'模块,我想创建一些Rest控制器来处理这些数据。我将向您展示目前为止的模块结构和文件:

核心模块:

|-- 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
    |-- 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

这是Whitelabel页面上显示的内容:

  

Whitelabel错误页面

     

此应用程序没有/ error的显式映射,因此您将此视为后备。

     

Thu May 31 20:09:48 EEST 2018

     

出现意外错误(type = Not Found,status = 404)。

     

无可用信息

1 个答案:

答案 0 :(得分:0)

@SpringBootApplication注释相当于使用@Configuration@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组件

enter image description here

See the docks