OpenFeign service returns null objects, the caller got a specific object, but the field is null
problem of environmental background and own tried what method
When doing the registration function through the Auth module pass the username to find the user module parameters, and returns the user object (whether or not null), then test the content of the user module returns, according to whether it is empty for the logic behind the operation, but when I returned to the user module the user object is null when the Auth module to get the user object is not null, but his field to null to empty operation is not hit. Now is by getting returned userId to empty for alternative solution
Related code
AuthController
@ PostMapping ("/regist/password ")
Public String regist (@ RequestBody @ Valid UserRegistByPasswordReqVo UserRegistByPasswordReqVo,
BindingResult result) {
If (result. HasErrors ()) {
Throw new BusinessException (" 1 ", Objects. RequireNonNull (result) getFieldError ()). GetDefaultMessage ());
}
User User=userService. FindUser (userRegistByPasswordReqVo getUsername ());
//the current alternative
//if the user. The getId ()!=null) {
//throw new AuthException (.//AuthErrorCode ACCOUNT_EXIST_USERNAME);
}
//by judging whether the user is null solution
Optional. OfNullable (user). IfPresent (u - & gt; {
If (u.g etId ()!=null) {throw new AuthException (AuthErrorCode ACCOUNT_EXIST_USERNAME); }
});
UserService. AddUser (userRegistByPasswordReqVo);
Return "success";
}
Auth module UserService
@ FeignClient (" demos - the user ")
Public interface UserService {
@ GetMapping ("/user/{username} ")
The User findUser (@ PathVariable String username);
@ PostMapping ("/user ")
Void addUser (UserRegistByPasswordReqVo UserRegistByPasswordReqVo);
}
UserController
@ PostMapping
@ IgnoreResponseAdvice (errorDispose=false)
Public User addUser (@ RequestBody UserRegistByPasswordReqVo userRegistByPasswordReqVoVo) {
Return userService. AddUser (userRegistByPasswordReqVoVo);
//throw new UserException (UserErrorCode TEST_ERROR);
}
@ GetMapping ("/{username} ")
Public User findUser (@ PathVariable String username) {
Try {
Thread.sleep (2000 l);
{} catch InterruptedException (e)
e.printStackTrace();
}
Return userService. FindByUsername (username);
}
What are you looking forward to? What is actually see the error message?
Looking forward to the result:
When the userService. FindUser () returns Null when
Optional. OfNullable (user) ifPresent (u - & gt; {
If (u.g etId ()!=null) {throw new AuthException (AuthErrorCode ACCOUNT_EXIST_USERNAME); }
});
Don't throw an exception, the user is null
Actually see the error:
FindUser query the object is null,
Through the debug can see that the user is null, no address values, and show the caller breakpoint is address values, field is null.
CodePudding user response:
The role of Optional object is null, the new one objectCodePudding user response:
Make sure userService. Whether findByUsername returns nullThen confirm the project feign blocker did what processing
CodePudding user response: