【问题标题】:Bad Request 400 when trying to get all users. Dto and form尝试获取所有用户时出现错误请求 400。 Dto 和形式
【发布时间】:2021-07-23 19:37:49
【问题描述】:

我有带有用户模型的 REST api - DTO 和创建/更新表单。我的 userService 检查用户是否是管理员,然后允许在 List 中获取所有用户。当我想获取所有用户时,我收到错误请求 400,但它应该返回 Forbidden。它曾经可以工作,但是当我对代码添加一些更改时,我收到了错误的请求。我不知道我错过了什么......

我的用户.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;
    }

用户控制器

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

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;

【问题讨论】:

  • 您使用的网址是什么?调试日志说明了什么?
  • 你的http请求怎么样?要求真的不好吗?我认为然后错误响应代码是从控制器返回的,程序没有进入服务。
  • localhost:8080/users - 已解决 [org.springframework.web.bind.MissingServletRequestParameterException 方法参数类型 UUID 所需的请求参数“uuid”不存在]
  • 听起来您的网址应该是 /users?uuid=...

标签: java spring spring-boot rest


【解决方案1】:

我认为您错过了请求标头中的 uuid 参数。 会是这样。 http://localhost:8080/users?uuid="enter_your_uuid_here"

【讨论】:

  • 啊,我错过了!我以为即使请求标头中没有 uuid 参数,我也会收到禁止。谢谢
【解决方案2】:

您的 JSON 请求数据可能与您的 DTO 数据字段不匹配。 验证 JSON 请求中的以下几点

  • 可能存在字段名称错误的问题
  • 可能没有按照数据类型发送正确的数据。

【讨论】:

    猜你喜欢
    • 2019-06-23
    • 2019-10-10
    • 1970-01-01
    • 2020-12-07
    • 1970-01-01
    • 2011-10-25
    • 2023-02-10
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多