使用Spring MVC构建WebService时Get请求返回空参数问题排查
Hey there, let's figure out why your GET request is returning empty parameters for your Spring MVC WebService. Based on the code snippets you shared, here are the most common issues to check and fix:
1. Verify Controller Request Mapping & Parameter Binding
Your controller code is cut off, but the first thing to confirm is that your request handling method correctly maps the GET request and binds parameters properly:
- Make sure you're using
@GetMapping(or@RequestMapping(method = RequestMethod.GET)) on your controller method. - Use the right annotation for your parameter type:
- For path variables (e.g.,
http://your-api/adtab/123/456), use@PathVariable:@GetMapping("/adtab/{window_ID}/{tab_ID}") public ResponseEntity<VO> fetchADTab( @PathVariable("window_ID") String windowId, @PathVariable("tab_ID") String tabId ) throws CompiereAPIApplicationException { VO tabData = adTabService.read(windowId, tabId); return ResponseEntity.ok(tabData); } - For query parameters (e.g.,
http://your-api/adtab?window_ID=123&tab_ID=456), use@RequestParam(ensure thenamematches your request parameter exactly, including case):@GetMapping("/adtab") public ResponseEntity<VO> fetchADTab( @RequestParam(name = "window_ID") String windowId, @RequestParam(name = "tab_ID") String tabId ) throws CompiereAPIApplicationException { VO tabData = adTabService.read(windowId, tabId); return ResponseEntity.ok(tabData); }
- For path variables (e.g.,
2. Check Your BaseWs Base Class Logic
Since your ADTabWs extends BaseWs<IADTabS..., confirm the base class isn't interfering with parameter binding:
- Ensure the base class doesn't override Spring MVC's default parameter handling (e.g., custom request interceptors or parameter parsing that drops values).
- Verify that any shared logic in
BaseWscorrectly passes parameters down to your service methods.
3. Validate Parameter Flow to Your Service
Add debug logs in your controller to confirm if parameters are even reaching the controller layer:
@GetMapping("/adtab") public ResponseEntity<VO> fetchADTab( @RequestParam(name = "window_ID") String windowId, @RequestParam(name = "tab_ID") String tabId ) throws CompiereAPIApplicationException { // Log to check if parameters are received System.out.println("Received window_ID: " + windowId + ", tab_ID: " + tabId); VO tabData = adTabService.read(windowId, tabId); return ResponseEntity.ok(tabData); }
- If logs show empty values here: The issue is with parameter binding (go back to point 1).
- If logs show valid values but the response is empty: The problem is in your
readservice method (check if it's returning a null/emptyVO).
4. Ensure Proper Serialization of Your VO Class
If your service returns a valid VO but the response is empty, Jackson (Spring's default serializer) might not be able to process the VO class:
- Add a public no-arg constructor to your
VOclass (Jackson requires this to instantiate objects). - Add public getter methods for all fields you want to include in the response, or use
@JsonPropertyon private/protected fields to explicitly mark them for serialization.
5. Confirm Request Format Correctness
Double-check your GET request matches the expected format:
- For path variables: Ensure the URL uses the exact path structure your
@GetMappingdefines (e.g.,/adtab/10000/20000). - For query parameters: Ensure parameter names match exactly (HTTP query params are case-sensitive, so
window_ID!=window_id).
内容的提问来源于stack exchange,提问作者zebiri djallil

