java - 此应用程序没有显式映射 /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);
}
}
解决方案
看看你的包结构主类DemoApplication
在com.example.demo
包中
package com.example.demo;
@SpringBootApplication
public class DemoApplication {
并且CarService
在package service.CarService
import service.CarService;
@Autowired
CarService cs;
同样的方式CarController
可能在不同的包中,这些包没有组织结构。
默认情况下@SpringBootApplication
,从声明此注解的类的包中执行组件扫描扫描。由于您的service
和controller
类不在基本包的子包中,因此您需要显式添加@ComponentScan
@SpringBootApplication
@ComponentScan({"service","controller"})
public class DemoApplication {
推荐阅读
- php - PHP 7.1 是否认为 0 未定义?
- android - network_security_config 的配置
- wordpress - 在 Laravel 应用程序中安装 Wordpress - 永久链接不起作用
- kubernetes - Istio VirtualService 总是 404s
- php - 计算拟合框数的算法
- kotlin - 尽管至少存在一个 Creator,但无法构造类的实例
- django - Django 自定义验证
- kotlin - 使用 kapt 进行多轮注释处理
- python - python中的偏移行函数?
- java - 使用策略设计模式创建单例 - Android