如何在WildFly 9中通过Java访问未解压WAR内的JSON资源文件
Hey there, let's work through this issue together—unexploded WARs can sometimes throw curveballs with resource access, but we've got several actionable fixes to try:
1. Verify JSON File Placement & Accessibility Rules
- First, double-check where your JSON file lives in the WAR:
- If it's inside
WEB-INF/orWEB-INF/classes/, you can't access it directly via a browser URL—WildFly blocks direct external access to theWEB-INFdirectory for security reasons. Move it to the WAR root (or a subdirectory outsideWEB-INF) if you need to hit it directly. - If it's in the WAR root or a public subdir, make sure the path matches what you're using in your URL.
- If it's inside
2. Confirm Your Access URL is Correct
Let's say your WAR's context root is myapp, and your JSON file is at myapp/data/config.json in the WAR structure. Your full access URL should look like:http://your-server:8080/myapp/data/config.json
- If you didn't set a custom context root, WildFly uses the WAR filename by default (e.g.,
myapp.warbecomes/myapp).
3. Check WildFly Deployment Configuration
Ensure WildFly is properly set up to handle zipped WARs:
- Open your
standalone.xml(ordomain.xmlfor domain mode) and locate the<deployment-scanner>section. Verify these settings:
The<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000" auto-deploy-zipped="true"/>auto-deploy-zipped="true"flag is critical here—it tells WildFly to deploy zipped WARs without unpacking them.
4. Fix Programmatic Resource Access (If Using Java Code)
If you're trying to read the JSON file from your application code, never use absolute file paths—they won't work with unexploded WARs. Instead, use the class loader:
// For a file in WEB-INF/classes/config.json InputStream jsonStream = getClass().getResourceAsStream("/config.json"); // For a file in WEB-INF/classes/data/config.json InputStream jsonStream = getClass().getResourceAsStream("/data/config.json");
This method works regardless of whether the WAR is exploded or not.
5. Clean WildFly Cache & Redeploy
Cached deployment data can sometimes cause access issues:
- Stop WildFly completely.
- Delete the contents of
standalone/tmpandstandalone/data/content(these directories hold temporary deployment files). - Restart WildFly and redeploy your WAR.
6. Check File Permissions
Make sure the user running WildFly has read permissions on your zipped WAR file. If the server can't read the WAR, it won't be able to access any resources inside it.
Give these steps a try—one of them should get your JSON resources accessible again!
内容的提问来源于stack exchange,提问作者Selvakumar

