白标错误页面只需输入 URL? [英] Whitelabel Error Page just type on URL?
问题描述
这个 Spring-boot 项目与 Angular 4 集成.输入url地址可以进入首页>> localhost:8080
.
This Spring-boot project is integrated with Angular 4. I can reach the home page when type the url adress >> localhost:8080
.
当输入任何不同的 url 添加参数时,我得到 whitelabel 错误页面,例如:/login,/signup.
I get whitelabel error page when type any diffent url is added parameter like: /login, /signup.
出现意外错误(类型=未找到,状态=404).
There was an unexpected error (type=Not Found, status=404).
我可以使用 angular-routinglocalhost:8080/signup、localhost:8080/foo
、..)>.所以问题是直接打url.
I can reach these pages (localhost:8080/signup
, localhost:8080/foo
, ..) on website using angular-routing. So the problem is about only hitting url directly.
那么我该如何解决这个问题,任何检查的想法都会有所帮助.
So How can I solve this, any idea to check would be helpful.
注意:这些url在服务器端没有授权.
Note: There is no authorization for these url in server side.
添加了 index.html 路径.
index.html path added.
src/main/resources
static
assets
index.html
bundle
bundle
..
routing.ts
export const routes: Routes = [
{
path: '',
component: HomeComponent,
pathMatch: 'full'
},
{
path:'signup',
component: SignupComponent,
canActivate: [GuestGuard],
pathMatch:'full'
},
推荐答案
我已经直接实现了这个 WebMvcConfigurer
并且它非常适合我.这是参考链接.
I have implemented directly this WebMvcConfigurer
and it works perfectly for me. This is the reference link.
import java.io.IOException;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**/*")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath,
Resource location) throws IOException {
Resource requestedResource = location.createRelative(resourcePath);
return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
: new ClassPathResource("/static/index.html");
}
});
}
}
这篇关于白标错误页面只需输入 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!