Skip to content

Bad Request 400 when trying to get all users. Dto and form

I have REST api with User model – DTO and Create / update form. My userService checks if user is administrator, then allow to getAllUsers in List. When I want to get all users, I get Bad request 400, but it should return Forbidden. It used to work but when I added some changes to my code I got bad request. I don’t know what I’m missing…

My User.java

///Lombok annotations
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Setter(AccessLevel.NONE)
    @Column(unique = true)
    private Long id;

    @Setter(AccessLevel.NONE)
    @EqualsAndHashCode.Include
    @Column(nullable = false, unique = true)
    private UUID uuid = UUID.randomUUID();

    @Column(unique = true, nullable = false, length = 254)
    private String login;
    @Column(nullable = false, length = 254)
    private String firstName;
    @Column(nullable = false, length = 254)
    private String lastName;
    @Enumerated(EnumType.STRING)
    private RoleType roleType;
    @Column(nullable = false, length = 254)
    private String password;
    @Email
    @Column(nullable = false, length = 254)
    private String email;
    @Positive
    private Double cost;

    public User(String login, String firstName, String lastName, RoleType roleType, String password,
                String email, Double cost) {
        this.login = login;
        this.firstName = firstName;
        this.lastName = lastName;
        this.roleType = roleType;
        this.password = password;
        this.email = email;
        this.cost = cost;
    }

UserController

@GetMapping("users")
    public ResponseEntity<List<UserDto>> getAllUsers(@RequestParam UUID uuid) {
        return userService.getListResponseEntity(uuid);
    }

UserService

public ResponseEntity<List<UserDto>> getListResponseEntity(UUID adminUuid) {
        if (authService.adminAuth(adminUuid)) {
            List<User> users = userRepo.findAll();
            List<UserDto> userDto = users
                    .stream()
                    .map(user -> userMapper.mapToUserDto(user))
                    .collect(Collectors.toList());

            return new ResponseEntity<>(userDto, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        }
    }

UserDto

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class UserDto {

    private String login;
    private String firstName;
    private String lastName;
    private RoleType roleType;
    private String email;
    private Double cost;

Answer

I think you missed uuid parameter in request header. It will be like this. http://localhost:8080/users?uuid="enter_your_uuid_here"