试图获取所有用户时,java错误请求400。Dto和表格
我有RESTAPI和用户模型DTO以及创建/更新表单。My userService检查用户是否为管理员,然后允许在列表中获取用户。当我想要获得所有用户时,我得到了错误的请求400,但它应该返回禁止。它过去是可以工作的,但当我在代码中添加一些更改时,我收到了错误的请求。我不知道我错过了什么
我的用户。爪哇
///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;
}
用户控制器
@GetMapping("users")
public ResponseEntity<List<UserDto>> getAllUsers(@RequestParam UUID uuid) {
return userService.getListResponseEntity(uuid);
}
用户服务
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);
}
}
用户数据
@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;
# 1 楼答案
我认为您在请求头中遗漏了uuid参数。 会是这样的
http://localhost:8080/users?uuid="enter_your_uuid_here"
# 2 楼答案
可能存在JSON请求数据与DTO数据字段不匹配的更改。 验证JSON请求中的以下几点