配置响应在Spring启动应用程序上提供 [英] Configure react to be served on Spring boot app
问题描述
到目前为止,我一直在为我的Spring启动应用编写一堆端点,没有html UI端。现在,我想提供包含反应代码的HTML文件和js文件。
So far, I've been writing a bunch of endpoints for my Spring boot app, with no html UI side. Now, I want to serve the HTML file and js files which contain react code.
当我访问 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
到目前为止我做了什么:
What I did so far:
1.增加了一个扩展的Web配置类 WebMvcConfigurerAdapter
:
1.Added a web config class that extends 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;
}
}
2.增加了一个休息控制器端点:
2.Added a rest controller endpoint:
@RestController
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView getdata() {
ModelAndView model = new ModelAndView("index");
return model;
}
}
我有 myproject / src /main/resources/static/index.html
我项目中的文件。
推荐答案
Spring Boot can自动处理静态文件(按照惯例),只需将所有html,js,css等文件放到 src / main / resources / static
中,删除你的ViewResolver和Controller对于'/'并且它将起作用, index.html
也将被Spring Boot映射到 /
。
Spring Boot can automatically handle static files (by convention), just put all of your html, js, css, etc. files to src/main/resources/static
, remove your ViewResolver and Controller for '/' and it will work, index.html
will also be mapped to /
by Spring Boot as well.
除此之外,您当然可以通过使用正确的 @RequestMapping
来创建带有api前缀的REST端点你的 @RestController
s
Besides this, you can of course make REST endpoints with the api prefix by just using the correct @RequestMapping
on your @RestController
s
这篇关于配置响应在Spring启动应用程序上提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!