【问题标题】:Spring Boot \ JWT: The method ok(T) in the type ResponseEntity is not applicable for the arguments (JwtResponse)Spring Boot \ JWT:ResponseEntity类型中的方法ok(T)不适用于参数(JwtResponse)
【发布时间】:2021-04-04 05:56:39
【问题描述】:

我下面有这个方法。

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));
}

【问题讨论】:

    标签: java spring spring-boot spring-security


    【解决方案1】:

    应该是:

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

    【讨论】:

    • 我其实是在寻找你的错误背后的原因,但是我发现你写的很正常,例如如果你进入ok(T)方法你会发现和@987654323完全相同的实现@ 让人难以理解!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    相关资源
    最近更新 更多