首页 > 解决方案 > 此应用程序没有显式映射 /error 白标错误

问题描述

在确保主应用程序位于正确的包中后,此应用程序没有显示 /error 的显式映射

确保应用程序和服务以及控制器在正确的包中尝试使用组件扫描以及检查依赖关系

https://ibb.co/n7vhZLD:文件/包顺序

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class DemoApplication {

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

}

依赖项

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<packaging>war</packaging>

控制器

import java.util.ArrayList;
import java.util.Hashtable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import model.Car;
import service.CarService;

@CrossOrigin
@RestController
@RequestMapping("/car")
public class CarController {

@Autowired
CarService cs;

@RequestMapping("/all")
public ArrayList<Car> getAll() {
    return cs.getAll();
}

@RequestMapping("{id}")
public Car getCar(@PathVariable("id") String id) {
    return cs.getCar(id);
}
}

标签: javamavenspring-boottomcat

解决方案


看看你的包结构主类DemoApplicationcom.example.demo包中

package com.example.demo;

@SpringBootApplication
public class DemoApplication {

并且CarServicepackage service.CarService

import service.CarService;

@Autowired
CarService cs;

同样的方式CarController可能在不同的包中,这些包没有组织结构。

默认情况下@SpringBootApplication,从声明此注解的类的包中执行组件扫描扫描。由于您的servicecontroller类不在基本包的子包中,因此您需要显式添加@ComponentScan

@SpringBootApplication
@ComponentScan({"service","controller"})
public class DemoApplication {

推荐阅读