Home > Back-end >  OpenFeign service returns null objects, the caller got a specific object, but the field is null
OpenFeign service returns null objects, the caller got a specific object, but the field is null

Time:12-17

description
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 object

CodePudding user response:

Make sure userService. Whether findByUsername returns null
Then confirm the project feign blocker did what processing

CodePudding user response:

refer to the second floor RockeyCui response:
make sure userService. Whether findByUsername returns null
Then confirm project feign blocker to do any processing
screenshots have identified is null, then feign didn't do the interceptor configuration in code

CodePudding user response:

reference 1st floor KeepSayingNo response:
Optional action object is Null, the new one object to
can see AuthController code, but before the Optional function printed object is not Null? He wouldn't throw an exception if is empty
  • Related