第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

嘗試從我的 SpringBoot 應(yīng)用程序提供靜態(tài)內(nèi)容,但失敗

嘗試從我的 SpringBoot 應(yīng)用程序提供靜態(tài)內(nèi)容,但失敗

有只小跳蛙 2023-10-13 16:41:30
我想從我的 SpringBoot 應(yīng)用程序提供靜態(tài)文件。我有一個(gè)非常簡(jiǎn)單的控制器,我希望它能完成以下任務(wù):@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 文件位于靜態(tài)文件夾中:MyApp/   src/      main/         static/index.html         static/img/image.png當(dāng)我使用curl向http://localhost:8080發(fā)出GET請(qǐng)求時(shí),我得到響應(yīng)代碼404作為返回,并且服務(wù)器狀態(tài)No mapping for GET /。我期望返回index.html 文件。http://localhost:8080/hello不過,使用MyBody對(duì)象作為 json 正文發(fā)送 POST 請(qǐng)求是可行的!我做錯(cuò)了什么?我已經(jīng)在 Spring 網(wǎng)站上讀過這篇博文,但自從該博文于 2013 年發(fā)布以來,它似乎已經(jīng)很老了。也許今天它的工作方式有所不同?
查看完整描述

5 回答

?
慕勒3428872

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

所以你應(yīng)該刪除@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());

? ?}

}



查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
HUH函數(shù)

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超4個(gè)贊

您不應(yīng)該在 Spring Boot 中使用 EnableWebMvc。


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

我認(rèn)為您缺少資源文件夾,您的文件夾結(jié)構(gòu)應(yīng)該如下所示


MyApp/

    src/

      main/

        resources/

          static/index.html

          static/img/image.png


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

靜態(tài)資源通常位于 /src/main/resources 下以進(jìn)入 maven 標(biāo)準(zhǔn)項(xiàng)目布局中的類路徑,Spring Boot 應(yīng)該為 /static (/src/main/resources/static) 下的所有文件提供服務(wù),而無需任何應(yīng)用程序配置addResourceHandler()

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


查看完整回答
反對(duì) 回復(fù) 2023-10-13
?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

我有一個(gè)博客應(yīng)用程序,它在 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 中做了這個(gè):


@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()

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 5 回答
  • 0 關(guān)注
  • 197 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)