我正在使用Spring Boot后端构建单页Web应用程序,并且理想情况下要共享前端使用的JSON文件作为各种类型的路由枚举,并通过后端来共享支持将某些路线映射回/index.html
这是JSON文件:
{
"Login": "/login",
"ForgotPassword": "/forgot-password",
"ResetPassword": "/reset-password",
"Profile": "/profile",
"Configuration": "/configuration",
"Administration": "/admin"
}
到目前为止,我一直在Node.js中这样做:
for (var pathname in Path) {
if (Path.hasOwnProperty(pathname)) {
app.get(Path[pathname], sendIndex);
}
}
目前我有这个:
@Configuration
public class SpringConfigurations extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
}
是否有办法将JSON文件注入java.util.Map<String,String>
,然后执行以下操作:
@Configuration
public class SpringConfigurations extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
pathsMap.values().forEach(path -> {
registry.addResourceHandler(path)
.addResourceLocations("classpath:/static/index.html");
});
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
}
答案 0 :(得分:0)
Json Jackson library(具体来说,它的bind module)能够将Json文件加载并解析为Map:
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> pathsMap = (Map<String, Object>)mapper.readValue(jsonSource, Map.class);
如果您事先知道所有值都是字符串,则可以将地图定义为<String, String>
readValue()
重载了接受Reader,Stream,pre-loadeded String等的变体。