Angular 4 WAR包部署JBOSS报404,base-href配置求助
Hey there, let's work through this 404 issue you're hitting with your Angular 4 and Java REST API project on JBOSS. I've debugged similar deployment headaches before, so let's break down the most likely fixes step by step:
1. Correctly Set base-href During Angular Build
Changing the <base href> in index.html manually might not be enough—Angular's build process can override this if you don't specify it explicitly. Always use the build flag to set your base path, which should match your JBOSS context root:
ng build --prod --base-href="/YOUR_WAR_FILENAME/"
Replace YOUR_WAR_FILENAME with the exact name of your WAR file (without the .war extension). For example, if your WAR is my-angular-app.war, use --base-href="/my-angular-app/".
2. Verify JBOSS Context Root Configuration
JBOSS uses the WAR filename as the default context root unless you explicitly set it. To confirm or customize this:
- Option 1: Use WAR filename as context root
Ensure your WAR file name matches the path you're using inbase-href. If your WAR ismy-app.war, your app should be accessible athttp://localhost:8080/my-app/. - Option 2: Customize context root in
web.xml
Add a<context-root>element to yourweb.xmlto define a custom path:
If you use this, update your build command to match:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <context-root>/my-custom-path</context-root> <!-- Your other web.xml configurations --> </web-app>ng build --prod --base-href="/my-custom-path/".
3. Check JBOSS Deployment Logs for Errors
404s sometimes hide underlying deployment issues. Look into JBOSS's server logs (usually at standalone/log/server.log) for any errors during WAR deployment—like missing dependencies, invalid XML configurations, or permission issues. These can prevent your app from being served correctly even if the base path looks right.
4. Validate Static Resource Access
Test if your Angular static files are being served properly. Try accessing a direct URL like:
http://localhost:8080/YOUR_CONTEXT_ROOT/styles.bundle.js
If this returns a 404, your WAR file might not include the built Angular assets. Double-check that your build output (from dist/ folder) is correctly packaged into the WAR's root directory (not nested inside a subfolder).
5. Adjust Angular Routing Strategy (Temporary Test)
If you're using Angular's default PathLocationStrategy, JBOSS might not be routing deep URLs correctly to index.html. As a temporary test, switch to HashLocationStrategy which uses # in URLs to avoid server-side routing issues:
In your AppModule:
import { HashLocationStrategy, LocationStrategy } from '@angular/common'; @NgModule({ // ... other module imports and declarations providers: [ { provide: LocationStrategy, useClass: HashLocationStrategy } ], }) export class AppModule { }
With this, your URLs will look like http://localhost:8080/YOUR_CONTEXT_ROOT/#/dashboard—if this works, the issue is likely with JBOSS not handling HTML5 routing (you'll need to configure a rewrite rule or welcome file fallback for the default strategy).
6. Ensure web.xml Has Correct Welcome File
Make sure your web.xml includes index.html as a welcome file to ensure JBOSS serves it by default:
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
Start with steps 1 and 2—those are the most common culprits for base path-related 404s in JBOSS deployments. Let me know if any of these steps help, or if you find specific errors in the logs!
内容的提问来源于stack exchange,提问作者Nishant Varshney

