gpt4 book ai didi

java - 此应用程序没有明确映射/错误白标错误

转载 作者:行者123 更新时间:2023-11-28 23:12:36 25 4
gpt4 key购买 nike

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

确保应用程序、服务和 Controller 在正确的包中尝试使用组件扫描并检查依赖性

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>

Controller

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);
}
}

最佳答案

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

package com.example.demo;

@SpringBootApplication
public class DemoApplication {

CarService包service.CarService

import service.CarService;

@Autowired
CarService cs;

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

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

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

关于java - 此应用程序没有明确映射/错误白标错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55432161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com