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

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

嘗試從我的 SpringBoot 應用程序提供靜態內容,但失敗

嘗試從我的 SpringBoot 應用程序提供靜態內容,但失敗

有只小跳蛙 2023-10-13 16:41:30
我想從我的 SpringBoot 應用程序提供靜態文件。我有一個非常簡單的控制器,我希望它能完成以下任務:@EnableWebMvc@RestControllerpublic class MyRestController implements WebMvcConfigurer {   @Override   public void addResourceHandlers(ResourceHandlerRegistry registry) {       registry.addResourceHandler("/static/**")               .addResourceLocations("classpath:/static")               .addResourceLocations("file:/static");   }   @PostMapping(path = "/hello")   public MyResponse hello(@RequestBody() MyBody body,                         HttpServletRequest request) {       return new MyResponse("Hello " + request.getRemoteAddr());   }}我的index.html 文件位于靜態文件夾中:MyApp/   src/      main/         static/index.html         static/img/image.png當我使用curl向http://localhost:8080發出GET請求時,我得到響應代碼404作為返回,并且服務器狀態No mapping for GET /。我期望返回index.html 文件。http://localhost:8080/hello不過,使用MyBody對象作為 json 正文發送 POST 請求是可行的!我做錯了什么?我已經在 Spring 網站上讀過這篇博文,但自從該博文于 2013 年發布以來,它似乎已經很老了。也許今天它的工作方式有所不同?
查看完整描述

5 回答

?
慕勒3428872

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

Spring Boot?Documentation中提到了這一點,在 spring mvc 部分下您可以使用 WebMvcConfigurer,但不需要執行 @EnableWebMvc

所以你應該刪除@EnableWebMvc注釋!

//@EnableWebMvc Remove this

@RestController

public class MyRestController implements WebMvcConfigurer {


? ?@Override

? ?public void addResourceHandlers(ResourceHandlerRegistry registry) {

? ? ? ?registry.addResourceHandler("/static/**")

? ? ? ? ? ? ? ?.addResourceLocations("classpath:/static")

? ? ? ? ? ? ? ?.addResourceLocations("file:/static");

? ?}


? ?@PostMapping(path = "/hello")

? ?public MyResponse hello(@RequestBody() MyBody body,

? ? ? ? ? ? ? ? ? ? ? ? ?HttpServletRequest request) {

? ? ? ?return new MyResponse("Hello " + request.getRemoteAddr());

? ?}

}



查看完整回答
反對 回復 2023-10-13
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

您不應該在 Spring Boot 中使用 EnableWebMvc。


查看完整回答
反對 回復 2023-10-13
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

我認為您缺少資源文件夾,您的文件夾結構應該如下所示


MyApp/

    src/

      main/

        resources/

          static/index.html

          static/img/image.png


查看完整回答
反對 回復 2023-10-13
?
哆啦的時光機

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

靜態資源通常位于 /src/main/resources 下以進入 maven 標準項目布局中的類路徑,Spring Boot 應該為 /static (/src/main/resources/static) 下的所有文件提供服務,而無需任何應用程序配置addResourceHandler()

https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html


查看完整回答
反對 回復 2023-10-13
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

我有一個博客應用程序,它在 static/ 之外的文件夾中接收上傳的圖像。


MyApp/

src/

  main/

    resources/

          static/

              css/

              javascript/

              images/

          blog/

              1-blogpost/ (here are the uploaded images)

              2-blogpost/ (here are the uploaded images)

              (... and so on)

所以我用 Spring Boot 在 Kotlin 中做了這個:


@Configuration

class WebConfiguration : WebMvcConfigurer {


    private val logger = loggerFactory.getLogger(WebConfiguration::class.java)


    override fun addResourceHandlers(registry: ResourceHandlerRegistry) {

        logger.info("####### Entering ResourceHandlers configurations #######")

        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/")

        registry.addResourceHandler("/blog/**").addResourceLocations("file:src/main/resources/blog/")

    }


    @Bean

    fun restTemplate() : RestTemplate {

        return RestTemplate()

    }

}


查看完整回答
反對 回復 2023-10-13
  • 5 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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