feat: add Vue 3 admin frontend and integrate with backend like mediabot
This commit is contained in:
39
par-api/src/main/java/com/par/api/config/WebMvcConfig.java
Normal file
39
par-api/src/main/java/com/par/api/config/WebMvcConfig.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.par.api.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* SPA 路由回退:所有非 API 非静态资源请求返回 index.html
|
||||
*/
|
||||
@Configuration
|
||||
public class WebMvcConfig implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/**")
|
||||
.addResourceLocations("classpath:/static/")
|
||||
.resourceChain(true)
|
||||
.addResolver(new PathResourceResolver() {
|
||||
@Override
|
||||
protected Resource getResource(String resourcePath, Resource location) throws IOException {
|
||||
Resource resource = location.createRelative(resourcePath);
|
||||
if (resource.exists() && resource.isReadable()) {
|
||||
return resource;
|
||||
}
|
||||
// API 路径不处理
|
||||
if (resourcePath.startsWith("api/")) {
|
||||
return null;
|
||||
}
|
||||
// 回退到 index.html(支持前端路由)
|
||||
return new ClassPathResource("static/index.html");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user