使用Kotlin+Spring Boot+Angular5构建REST应用时特定URL出现白标错误页
Hey there! Let's troubleshoot that Whitelabel Error Page you're hitting for your specific URL. Since your project runs fine otherwise, the issue is almost certainly tied to how that particular endpoint or frontend route is configured. Here are the most common fixes to check step by step:
- Double-check your Kotlin controller's path matching: Make sure the
@GetMapping(or corresponding HTTP method annotation) path exactly matches the URL you're accessing. Even tiny details like missing slashes, wrong case, or typos will trigger a 404. For example, if your controller uses@GetMapping("/api/customers/{id}")but you visit/api/customer/{id}, it'll fail. - Check path variables/request parameters: If the endpoint expects a path variable like
{userId}(e.g.,/api/users/123), ensure you're passing a valid value that matches the expected type (number vs string, etc.). Missing or invalid variables will throw an error. - Audit Spring Security or interceptors: If you have security configs, confirm the URL is allowed in your
SecurityFilterChain(viapermitAll()or proper role assignments). Interceptors might also be blocking the request—check if any custom interceptors are targeting this path.
If this URL is a frontend route (not a REST API call), the problem is likely that Spring Boot isn't forwarding client-side routes to Angular's index.html:
- Add a fallback controller in Spring Boot to redirect non-API requests to Angular's entry point. Here's a quick Kotlin implementation:
@Controller class RouteForwardController { // Catch all non-static, non-API requests and forward to index.html @GetMapping(value = ["/**/{path:[^\\.]*}"]) fun forwardToAngular(): String { return "forward:/index.html" } } - Confirm your Angular routing config: Open
app-routing.module.tsand make sure the route exists with the correct path and component. For example:const routes: Routes = [ { path: 'user-profile', component: UserProfileComponent }, // ... other routes ]; - Check your Angular build's
base-href: If you built Angular withng build, ensure you set the correct base href (e.g.,ng build --base-href /) to avoid path mismatches when deployed with Spring Boot.
- Validate Angular build files: Make sure your compiled Angular assets (in
dist/folder) are correctly copied to Spring Boot'ssrc/main/resources/staticorpublicdirectory. A failed build or missing files can cause routing failures. - Clear browser cache: Sometimes old cached assets or routing data can cause unexpected errors—hard refresh (Ctrl+F5) the page to rule this out.
Ensure the HTTP method you're using to access the URL matches what your backend endpoint expects. For example:
- If your controller uses
@PostMapping("/api/orders")but you're making a GET request to that URL, you'll get a 405 Method Not Allowed error, which triggers the Whitelabel page. Use tools like Postman to test the endpoint with the correct method.
Pro tip: Open your browser's DevTools (F12) and check the Network tab—look at the request's status code (404, 405, 500, etc.). This will instantly narrow down whether the issue is a missing resource, method mismatch, or backend error.
内容的提问来源于stack exchange,提问作者Luis Santiago Vázquez Mancilla

