到目前为止,我已经为Spring引导应用程序编写了许多端点,没有html UI端。现在,我要提供包含React代码的HTML文件和js文件。
当我访问时,http://localhost:8443我得到:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sun Nov 19 11:54:01 PST 2017
There was an unexpected error (type=Not Found, status=404).
Not Found
Run Code Online (Sandbox Code Playgroud)
到目前为止,我做了什么:
1.添加扩展网络配置类WebMvcConfigurerAdapter:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/resources/static/");
bean.setSuffix(".html");
return bean;
}
}
Run Code Online (Sandbox Code Playgroud)
2.添加了一个休息控制器端点:
@RestController
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView getdata() {
ModelAndView model = new ModelAndView("index");
return model;
}
}
Run Code Online (Sandbox Code Playgroud)
myproject/src/main/resources/static/index.html我的项目中有文件。
Spring Boot 可以自动处理静态文件(按照约定),只需将你所有的 html、js、css 等文件放到 中src/main/resources/static,删除你的 ViewResolver 和 Controller 为 '/' 就可以工作,index.html也会/被 Spring Boot映射到以及。
除此之外,您当然可以通过@RequestMapping在您的@RestControllers上使用正确的 api 前缀来创建 REST 端点
让我们看一个简单的案例:没有百里香模板或spring静态文件。Spring是用于提供rest api的,其余的则要做出反应。但是您可以在任何请求映射URL上使用控制器。
一种选择是ResourceResolver像这样使用和配置它:
@Configuration
public class Config implements WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
ResourceResolver resolver = new ReactResourceResolver();
registry.addResourceHandler("/**")
.resourceChain(true)
.addResolver(resolver);
// Can try to play with
// registry.addResourceHandler("/**")
// .addResourceLocations("classpath:/static/");
// But this option can't map every path to index.html
// Can try /sf/answers/3009917221/
// to resolve this, but then you loose /api/** => rest
// and to be honest it is some regex madness, so
// it was easier for me to setup custom resource resolver
}
public class ReactResourceResolver implements ResourceResolver {
// root dir of react files
// example REACT_DIR/index.html
private static final String REACT_DIR = "/static/";
// this is directory inside REACT_DIR for react static files
// example REACT_DIR/REACT_STATIC_DIR/js/
// example REACT_DIR/REACT_STATIC_DIR/css/
private static final String REACT_STATIC_DIR = "static";
private Resource index = new ClassPathResource(REACT_DIR + "index.html");
private List<String> rootStaticFiles = Arrays.asList("favicon.io",
"asset-manifest.json", "manifest.json", "service-worker.js");
@Override
public Resource resolveResource(
HttpServletRequest request, String requestPath,
List<? extends Resource> locations, ResourceResolverChain chain) {
return resolve(requestPath, locations);
}
@Override
public String resolveUrlPath(
String resourcePath, List<? extends Resource> locations,
ResourceResolverChain chain) {
Resource resolvedResource = resolve(resourcePath, locations);
if (resolvedResource == null) {
return null;
}
try {
return resolvedResource.getURL().toString();
} catch (IOException e) {
return resolvedResource.getFilename();
}
}
private Resource resolve(
String requestPath, List<? extends Resource> locations) {
if (requestPath == null) return null;
if (rootStaticFiles.contains(requestPath)
|| requestPath.startsWith(REACT_STATIC_DIR)) {
return new ClassPathResource(REACT_DIR + requestPath);
} else
return index;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是Spring 2.0.0.M4的完整工作演示:https : //github.com/varren/SpringBootReactExample
我有一些类似的问题,但设置有所不同:每个URL路由和子路由“ / a / ** => /a/index.html除外/ a / static / **”的Spring单页。
还有一个使用regex Spring捕获index.html的所有路由来部分解决问题的选项,但是我对这种方法没有运气
| 归档时间: |
|
| 查看次数: |
3922 次 |
| 最近记录: |