带有 Spring 引导的 SPA - 为非 API 请求提供 index.html
SPA with Spring Boot - serve index.html for non-API requests
我目前正在开发一个应该使用单页 React 前端的网页。对于后端,我使用 spring 引导框架。
所有 api 调用都应使用带有 /api
前缀的 url 并且应由 REST 控制器处理。
所有其他 url 应该只提供 index.html
文件。我将如何使用 spring 实现此目的?
实现您想要的最简单方法是实现自定义 404 处理程序。
将这些参数添加到您的 application.properties:
spring.resources.add-mappings=false
spring.mvc.throw-exception-if-no-handler-found=true
第一个 属性 删除所有默认静态资源处理,第二个 属性 禁用 Spring 的默认白标签页面(默认 Spring 捕获 NoHandlerFoundException
和提供标准的白标签页面)
将 404 处理程序添加到您的应用程序上下文:
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class PageNotFoundController {
@ExceptionHandler(NoHandlerFoundException.class)
public String handleError404() {
return "redirect:/index.html";
}
}
最后,您需要添加自定义视图解析器以提供静态内容(在本例中为index.html)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html").addResourceLocations("classpath:/static/index.html");
super.addResourceHandlers(registry);
}
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(InternalResourceView.class);
return viewResolver;
}
}
您的 index.html
应该放在 /resources/static/
目录中。
我目前正在开发一个应该使用单页 React 前端的网页。对于后端,我使用 spring 引导框架。
所有 api 调用都应使用带有 /api
前缀的 url 并且应由 REST 控制器处理。
所有其他 url 应该只提供 index.html
文件。我将如何使用 spring 实现此目的?
实现您想要的最简单方法是实现自定义 404 处理程序。
将这些参数添加到您的 application.properties:
spring.resources.add-mappings=false
spring.mvc.throw-exception-if-no-handler-found=true
第一个 属性 删除所有默认静态资源处理,第二个 属性 禁用 Spring 的默认白标签页面(默认 Spring 捕获 NoHandlerFoundException
和提供标准的白标签页面)
将 404 处理程序添加到您的应用程序上下文:
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class PageNotFoundController {
@ExceptionHandler(NoHandlerFoundException.class)
public String handleError404() {
return "redirect:/index.html";
}
}
最后,您需要添加自定义视图解析器以提供静态内容(在本例中为index.html)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html").addResourceLocations("classpath:/static/index.html");
super.addResourceHandlers(registry);
}
@Bean
public ViewResolver viewResolver() {
UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
viewResolver.setViewClass(InternalResourceView.class);
return viewResolver;
}
}
您的 index.html
应该放在 /resources/static/
目录中。