使用RestAssured时仅GET接口报404错误,其他请求正常
First, let's confirm the core issue: All your GET endpoints are returning a 404 Not Found error with RestAssured, while other HTTP methods (POST, PUT, DELETE) work as expected. The specific error you're seeing is:
java.lang.AssertionError: 1 expectation failed. Expected status code <200> but was <404>.
Here's your failing GET request code:
try { RestAssured.given() .header("Authorization","someToken") .when() .get(BASE_URI + "location/listAll") .then() .statusCode(200); } catch (Exception numberFormatException) { numberFormatException.getMessage(); }
And your working POST request for reference:
try { RestAssured.given() .log().all() .header("Authorization","someToken") .accept(MediaType.APPLICATION_JSON) .body(LocationModel) .when() .post(BASE_URI + "location/create") .then() .statusCode(200); } catch (Exception numberFormatException) { numberFormatException.getMessage(); }
Most Likely Causes & Fixes
1. Incorrect Path Spelling/Casing or Route Mismatch
Double-check the GET endpoint path against what your backend defines. Common mistakes here:
- Case sensitivity: If your backend uses case-sensitive routing (e.g., Spring Boot with default settings),
listAllvslistallwould be treated as different paths. Compare it to the working POST pathlocation/create—does your backend's GET route use the exact same casing? - Typos: Is it supposed to be
locations/listAll(plural) instead oflocation/listAll? Or maybelocation/list-all(hyphenated) instead of camelCase? - Missing/extra slashes: Ensure the path doesn't have leading/trailing slashes that clash with your BASE_URI.
2. BASE_URI and Path Concatenation Issue
This is a super common pitfall. If your BASE_URI doesn't end with a trailing slash (/), concatenating it with location/listAll will create an invalid URL. For example:
- If
BASE_URI = "http://your-api.com/api", the full GET URL becomeshttp://your-api.com/apilocation/listAll(missing a slash betweenapiandlocation), which is definitely a 404. - Compare this to your POST request—if the BASE_URI + "location/create" works, maybe the POST route accidentally works despite the slash issue (unlikely, but worth verifying).
Fix: Either add a trailing slash to your BASE_URI, or prepend a slash to your GET path:
// Option 1: Update BASE_URI to end with / private static final String BASE_URI = "http://your-api.com/api/"; // Option 2: Add leading slash to the path .get(BASE_URI + "/location/listAll")
3. Backend Route Method Binding Error
Make sure your backend is actually mapping a GET request to the location/listAll path. For example, in Spring Boot:
- A common mistake is using
@PostMappinginstead of@GetMappingfor the list endpoint. - Or maybe the GET route is mapped to a different path entirely (e.g.,
location/listinstead oflocation/listAll).
4. Debug with Request Logging
Your POST request uses .log().all() to print full request details—add this to your GET request to see exactly what URL and headers are being sent:
RestAssured.given() .log().all() // This will print the full request URL, headers, etc. .header("Authorization","someToken") .when() .get(BASE_URI + "location/listAll") .then() .statusCode(200);
This will confirm if the actual request URL matches what you expect. If it doesn't, that's your fix.
5. Test with External Tools
Rule out RestAssured as the issue by testing the GET endpoint directly with Postman, curl, or a browser. If you still get a 404, the problem is definitely on the backend side (not your test code). If it works in Postman but not RestAssured, check for header differences (e.g., missing Accept header in your GET request—your POST includes .accept(MediaType.APPLICATION_JSON); try adding that to your GET request too).
内容的提问来源于stack exchange,提问作者Swati Joshi

