Spring+AngularJS:邮箱查重时后端接收usrEmail参数为NULL问题排查
usrEmail Parameter in Email Duplicate Check Let’s walk through the most common reasons your backend is receiving a null usrEmail parameter and how to fix them:
1. Mismatch Between Request Data Type and Backend Annotation
Your backend uses @RequestParam, which is built to read query parameters or form-encoded data (from application/x-www-form-urlencoded requests). If your Angular frontend is sending a JSON payload (the standard approach for most SPAs), @RequestParam won’t be able to parse the value—this is almost certainly the root cause.
Fix for JSON Payloads:
Step 1: Update Backend to Use @RequestBody
First, create a simple DTO (Data Transfer Object) to hold the email value:
public class EmailCheckRequest { private String usrEmail; // Getters and setters public String getUsrEmail() { return usrEmail; } public void setUsrEmail(String usrEmail) { this.usrEmail = usrEmail; } }
Then modify your controller method to accept this DTO with @RequestBody:
import org.springframework.http.MediaType; @RequestMapping( value = "/api/v1/public/checkDuplicate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE ) public ResponseEntity<Object> getCnt( HttpServletRequest request, HttpServletResponse response, @RequestBody EmailCheckRequest emailRequest) { String usrEmail = emailRequest.getUsrEmail(); // Your duplicate check logic here }
Step 2: Ensure Frontend Sends Valid JSON
Update your Angular request to include the JSON payload and correct content type:
$scope.user.usrEmail = $scope.usrEmail; var req = { method: 'POST', url: '/api/v1/public/checkDuplicate', headers: { 'Content-Type': 'application/json' }, data: { usrEmail: $scope.user.usrEmail } // Send email as JSON object }; // Execute request with $http $http(req).then(function(response) { // Handle success }, function(error) { // Handle error });
2. If You Prefer Form-Encoded Data
If you want to stick with form-encoded data instead of JSON:
Backend Adjustment:
Explicitly declare your endpoint accepts form data:
@RequestMapping( value = "/api/v1/public/checkDuplicate", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE ) public ResponseEntity<Object> getCnt( HttpServletRequest request, HttpServletResponse response, @RequestParam(value="usrEmail", required=false) String usrEmail) { // Your logic here }
Frontend Adjustment:
Send data as form-encoded (use jQuery’s $.param or a custom encoding function):
var req = { method: 'POST', url: '/api/v1/public/checkDuplicate', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: $.param({ usrEmail: $scope.user.usrEmail }) }; $http(req).then(...);
3. Verify Parameter Name Spelling
Double-check that the frontend uses exactly usrEmail (case-sensitive!)—a typo like userEmail would instantly cause a null value.
4. Test Directly with Postman/Curl
To rule out frontend issues, test the endpoint directly:
- For JSON: Send a POST request with body
{"usrEmail": "test@example.com"}andContent-Type: application/json. - For form data: Send a POST request with form field
usrEmail=test@example.comandContent-Type: application/x-www-form-urlencoded.
If the backend receives the value correctly here, the problem lies in how your Angular app constructs the request.
内容的提问来源于stack exchange,提问作者zzangs33

