@feignclient无法获取空对象

js5cn81o  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(366)

我尝试从用户模块的usercontroller返回一个null对象(在idea中,它显示user=null),并在auth模块的userservice接口中获取该对象。然后在authcontroller中使用此服务。当我得到这个对象时,它不是空的(在idea中,它显示user=@x(内存地址值)和字段为空),我尝试使用 ObjectUtils.isEmpty(user) 检查它,它返回false。那么我怎样才能得到这个空对象呢?我的authcontroller是:

@RestController
@RequestMapping( "/auth" )
public class AuthController{

    @Autowired
    private AuthService authService;

    @Autowired
    private UserService userService;    

    @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() );
        Optional.ofNullable( user ).ifPresent( u -> {
            if( u.getId() != null ) {throw new AuthException( AuthErrorCode.ACCOUNT_EXIST_USERNAME );}
        } );

            userService.addUser( userRegistByPasswordReqVo );

        return "success";
    }

}

用户服务接口:

@FeignClient( "demos-user" )
public interface UserService{

    @GetMapping( "/user/{username}" )
    User findUser( @PathVariable String username );

    @PostMapping("/user")
    void addUser(  UserRegistByPasswordReqVo userRegistByPasswordReqVo );
}

用户模块的用户控制器:

@RestController
@RequestMapping( "/user" )
public class UserController{

    @Autowired
    private UserService userService;

    @PostMapping
    public User addUser( @RequestBody UserRegistByPasswordReqVo userRegistByPasswordReqVoVo ){
        return userService.addUser( userRegistByPasswordReqVoVo );
    }

    @GetMapping( "/{username}" )
    public User findUser( @PathVariable String username ){
        User byUsername = userService.findByUsername( username );
        return userService.findByUsername( username );
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题