5 回答

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

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

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

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()
}
}
添加回答
舉報(bào)