gpt4 book ai didi

java - SpringBoot WhiteLabel 更改包时出错

转载 作者:行者123 更新时间:2023-11-30 06:21:24 28 4
gpt4 key购买 nike

这是 demoApplication 类

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

这是 Controller

package com.example.demo.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController
{
@RequestMapping("/")
@ResponseBody
Response hellow() {
return new Response("Hello World!");
}

class Response{
private String message;

Response(String message){
this.setMessage(message);
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
}

如果我将 Controller 放在包 com.example.demo.controllers 上,就可以工作,但如果我将它放在 com.example.controllers 上,我会得到一个whiteLabelError,我假设在 com.example.controllers 中找不到 Controller ,我必须设置什么才能将 Controller 放在 com.example.controllers 上并且它可以工作?

最佳答案

发生这种情况是因为您的主类 DemoApplication 被注释为 @SpringBootApplication (在 as you can see in the source 下隐藏 @ComponentScan )位于包下com.example.demo 并默认扫描该包下的所有类以及下面的所有包。

因此,只需将 com.example 包下的 DemoApplication 移至更高一级,它就应该可以工作。

关于java - SpringBoot WhiteLabel 更改包时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48092514/

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