我正在使用Micronaut和Angular 4构建应用程序。
我已将Micronaut配置为提供静态资源
micronaut:
router:
static:
resources:
enabled: true
mapping: /**
paths: classpath:public
一切正常( DefaultURL路由:http://localhost:8080/dashboard )。 Angular应用程序已加载,用户与该应用程序进行交互并正确浏览路线。
在控制器中,我将服务器配置为重定向到index.html。如果服务器中不存在该路径。
@Get("/{[path:[^\\.]*}")
@Secured(SecurityRule.IS_ANONYMOUS)
@Produces(MediaType.TEXT_HTML)
public HttpResponse<?> refresh() {
return HttpResponse.redirect(URI.create("/index.html"));
}
但是当用户刷新页面时(例如,按F5键)。
如果刷新后当前网址为“ http://localhost:8080/userdetails/status” ,Angular应用将转到默认路由“ http://localhost:8080/dashboard” , 而不是用户在“ http://localhost:8080/userdetails/status”
中使用的路线请帮助我 谢谢
答案 0 :(得分:2)
找到下面已实施的工作代码。
@Controller("/")
public class IndexController {
@Inject
ResourceResolver res;
@Get("/{[path:[^\\.]*}")
@Secured(SecurityRule.IS_ANONYMOUS)
@Produces(MediaType.TEXT_HTML)
public HttpResponse<?> refresh(HttpRequest<?> request) {
StreamedFile indexFile = new StreamedFile(res.getResource("classpath:public/index.html").get());
return HttpResponse.ok(indexFile);
}
}
答案 1 :(得分:1)
这是因为您要重定向到/index.html
。先前请求中的信息丢失。除了重定向之外,您还应该直接呈现html。
有几种工具可以做到这一点,包括使用resourceResolver
来解析类路径上的html。您可以返回一个StreamedFile
实例,以将流呈现给响应。
可以注入解析器,您可以自己构建流文件。
https://docs.micronaut.io/latest/api/io/micronaut/core/io/ResourceResolver.html https://docs.micronaut.io/latest/api/io/micronaut/http/server/types/files/StreamedFile.html