spring boot\jwt:类型responseentity中的方法ok(t)不适用于参数(jwtresponse)

b91juud3  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(358)

我有下面这个方法。
jwtresponse是一个简单的pojo。
有人能解释一下为什么我在主题上有错误吗

ResponseEntity.ok(new JwtResponse...

如何修复?
谢谢

@PostMapping("/signin")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {

    Authentication authentication = authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()));

    SecurityContextHolder.getContext().setAuthentication(authentication);
    String jwt = jwtUtils.generateJwtToken(authentication);

    UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();      
    List<String> roles = userDetails.getAuthorities().stream()
            .map(item -> item.getAuthority())
            .collect(Collectors.toList());

    return ResponseEntity.ok(new JwtResponse(jwt, 
                                             userDetails.getId(), 
                                             userDetails.getUsername(), 
                                             userDetails.getEmail(), 
                                             roles));
}
rnmwe5a2

rnmwe5a21#

找到了!
应该是:

return ResponseEntity.ok().body(new JwtResponse(jwt, 
                userDetails.getId(), 
                userDetails.getUsername(), 
                userDetails.getEmail(), 
            roles));

相关问题