0

我想将 java 14 中的记录与实体(来自 java 持久性)一起使用。我尝试将我的用户映射到 userDto 并且我的调试器显示所有映射都是正确的。但不幸的是,当我想打印我的 userDto 时出现错误:

Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class xxx.UserMicroservice.dto.UserDto]

我做错了什么?

public interface UserMapper {
    UserMapper INSTANCE = Mappers.getMapper(UserMapper.class);

    UserDto mapToUserDto(User user);
}

public class UserMapperImpl implements UserMapper {

    @Override
    public UserDto mapToUserDto(User user) {
        if(user == null) {
            return null;
        }
        UserDto userDto = new UserDto(user.getName(), user.getEmail(), user.getImageUrl());
        return userDto;
    }

}
public record UserDto(
        String name,
        String email,
        String imageUrl) {}
@RestController
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/user/me")
    @PreAuthorize("hasRole('USER')")
    public UserDto getCurrentUser(@CurrentUser UserPrincipal userPrincipal) {
        UserDto userDto = UserMapper.INSTANCE.mapToUserDto(userRepository.findById(userPrincipal.getId())
                .orElseThrow(() -> new ResourceNotFoundException("User", "id", userPrincipal.getId())));
        return userDto;
    }
}
4

0 回答 0