此应用程序没有针对/ error白标签错误的显式映射

时间:2019-03-30 13:57:32

标签: java maven spring-boot tomcat

此应用程序没有针对/ 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);
}
}

1 个答案:

答案 0 :(得分:0)

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

package com.example.demo;

@SpringBootApplication
public class DemoApplication {

CarService位于package service.CarService

import service.CarService;

@Autowired
CarService cs;

以同样的方式,CarController可能位于不同的程序包中,而不是有组织的结构。

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

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