Angular 1.5调用接口遇javax.ws.rs.NotFoundException: HTTP 404错误求助
Hey there, let's break down this 404 error you're facing when calling your JAX-RS endpoint from Angular 1.5. Here are the most common fixes and checks to run through:
1. Double-Check the Full Request URL
First things first—make sure the URL your app is hitting is actually correct. Add a quick console log right before the $http.get call to see the full path:
console.log('Full endpoint URL:', env.domain + '/Voices/authorize'); $http.get(env.domain+'/Voices/authorize').then(...)
- Look for missing slashes (e.g.,
http://yourdomain.comVoices/authorizeinstead ofhttp://yourdomain.com/Voices/authorize) or extra characters from theenv.domainvariable. - Copy that full URL into your browser's address bar and hit enter. If you still get a 404, the problem is almost certainly on the backend side, not your Angular code.
2. Validate Backend JAX-RS Endpoint Setup
Now let's check the backend side to ensure the endpoint exists and is mapped correctly:
- Confirm your JAX-RS resource class has the right path annotations. It should look something like this:
@Path("/Voices") public class VoicesAuthorizationResource { @GET // Make sure this matches your Angular GET request @Path("/authorize") public Response getAuthorizationForUser() { // Your backend logic here } } - Watch out for case sensitivity! JAX-RS paths are case-sensitive by default. If your backend uses
/voices/authorize(lowercase "v") but your frontend calls/Voices/authorize, you'll get a 404. - Check if your backend is deployed with a context path (e.g.,
/api). If so, your full URL needs to include it:env.domain + '/api/Voices/authorize'.
3. Inspect the Request in Browser DevTools
Open your browser's DevTools (F12) and go to the Network tab. Find the failed authorize request and check:
- The Request URL to confirm it's what you expect.
- The Request Method is definitely
GET(matches your Angular$http.getcall). - The Response Body—sometimes backend servers send extra details about why the endpoint isn't found, even if the status code is 404.
4. Rule Out CORS and Deployment Issues
- While CORS errors usually show up as 403 or other status codes, it's worth confirming your backend allows cross-origin requests from your frontend domain. If you're making a cross-domain call, the browser sends an OPTIONS preflight request first—if your backend doesn't handle this, it might cause unexpected errors.
- Make sure your backend service is fully started and deployed correctly. If the server is down or the app didn't deploy properly, you'll get a 404 even with the right URL.
Quick Side Note on Your Angular Code
I noticed a tiny syntax issue in your success callback:
// self.isAdOps = response.data.;
That trailing dot after response.data will cause a syntax error once you uncomment that line. Make sure to fix it (e.g., self.isAdOps = response.data.isAdOps; or whatever property you need) once the 404 is resolved.
内容的提问来源于stack exchange,提问作者Elad Benda2

