亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot 顯示對索引的 http 請求,但不顯示任何其他映射

Spring Boot 顯示對索引的 http 請求,但不顯示任何其他映射

莫回無 2022-11-02 16:00:31
Spring Boot 顯示對索引路徑的 HTTP 請求,但不顯示任何其他映射。我創建了一個在 localhost:8060 上運行的 Spring Boot 應用程序。問題是,當我在瀏覽器上運行 localhost:8060 時,它可以工作,但是當我運行任何其他請求時,它會顯示“出現意外錯誤(type=Forbidden,status=403)。拒絕訪問'。在日志中它說它找到了正確的方法,即它正確映射但仍然拋出這個錯誤。問候.javaimport org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * Main application to launch spring boot. */@RestControllerpublic class Greetings {    /**     * Main application to launch spring boot.     */    @RequestMapping("/")    public String index() {        return "Greetings from Spring Boot!";    }    @RequestMapping("/testing123")    public String indextest() {        return "Testing";    }}服務器應用程序import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ApplicationContext;import org.springframework.scheduling.annotation.EnableAsync;/** * Main application to launch spring boot. */@SpringBootApplication@EnableAutoConfiguration@EnableAsyncpublic class ServerApplication {    private static Logger logger = LoggerFactory.getLogger(ServerApplication.class);    /**     * Start the Spring Boot application.     *     * @param args command line arguments     */    public static void main(String[] args) {        ApplicationContext context = SpringApplication.run(ServerApplication.class, args);        logger.info("Sample Application started with context {}", context.getDisplayName());    }}所以它確實顯示“來自 Spring Boot 的問候!” 當我去 localhost:8060 但在 localhost:8060/testing123 上拋出錯誤
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

因為您的主類ServerApplication位于另一個包中,該包是 Spring Boot 應用程序的基礎包。


但是控制器Greetings在不同的包中,而不是主類的子包,默認情況下,spring boot 應用程序將所有使用任何原型注釋的類從sub packages基包的任何構造型注釋作為springbean 加載到ApplicationContext


@ComponentScan在Main課堂上使用


@SpringBootApplication

@EnableAutoConfiguration

@EnableAsync

@ComponentScan({"com.vmware.skyscraper", "com.skyscraper.vdisizer"})

public class ServerApplication {


private static Logger logger = LoggerFactory.getLogger(ServerApplication.class);


/**

 * Start the Spring Boot application.

 *

 * @param args command line arguments

 */

public static void main(String[] args) {

    ApplicationContext context = SpringApplication.run(ServerApplication.class, args);

    logger.info("Sample Application started with context {}", context.getDisplayName());

  }

}


查看完整回答
反對 回復 2022-11-02
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號