Java Spring Boot\JWT:类型ResponseEntity中的方法ok(T)不适用于参数(JwtResponse)
我下面有这个方法 JwtResponse是一个简单的POJO 有人能解释一下我为什么会在主题上出错吗Java Spring Boot\JWT:类型ResponseEntity中的方法ok(T)不适用于参数(JwtResponse),java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我下面有这个方法 JwtResponse是一个简单的POJO 有人能解释一下我为什么会在主题上出错吗 ResponseEntity.ok(new JwtResponse... 如何修复它 谢谢 @PostMapping("/signin") public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) { Authentication a
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));
}
@PostMapping(/signin)
公共响应身份验证者(@Valid@RequestBody LoginRequest LoginRequest){
Authentication=authenticationManager.authenticate(
新的UsernamePasswordAuthenticationToken(loginRequest.getUsername(),loginRequest.getPassword());
SecurityContextHolder.getContext().setAuthentication(身份验证);
字符串jwt=jwtUtils.generateJwtToken(身份验证);
UserDetailsImpl userDetails=(UserDetailsImpl)身份验证。getPrincipal();
List roles=userDetails.getAuthorities().stream()
.map(项目->项目.getAuthority())
.collect(Collectors.toList());
返回响应正确(新jwt响应(jwt,
userDetails.getId(),
userDetails.getUsername(),
userDetails.getEmail(),
角色);;
}
找到了
应该是:
return ResponseEntity.ok().body(new JwtResponse(jwt,
userDetails.getId(),
userDetails.getUsername(),
userDetails.getEmail(),
roles));
实际上,我正在寻找错误背后的原因,但我发现您编写的是正常的,例如,如果您进入
ok(T)
方法,您将发现与ok().body(T)
完全相同的实现,这使您很难理解!