Spring Boot REST API约束ViolationException在密码字段上



我在弹簧后端有一个奇怪的问题,在那里我从我的angular2前端发送了一个带有域用户对象的邮政请求,该请求是由REST API端点收到的,并翻译它进入春季用户模型对象。问题在于,JSON请求中的密码字段似乎没有转换为后端上的密码字段。所有其他字段(用户名,名称,电子邮件等(都可以通过,但是密码为null。

请求有效载荷,如Chrome的网络选项卡

所示

电子邮件:" asdf@asdf.com"firstName:" asdfasdjk"lastname:" asdfs"登录:" adfsjk"密码:&quot" fasdfasdfasdsd"

从网络选项卡中看到的响应

错误:"内部服务器错误"例外:&quot javax.validation.constraintViolationException&quot&quot消息:"验证"在持续时间[javax.validation.groups.default,]

的持续时间[domain.user]失败[domain.user]

约束违规列表:[CondentArtaintViolationImpl {InterPoLatedMessage ='可能不为null',propertypath = password,rootbeanClass = class domain.user,messagetemplate ='{路径:"/api/用户"状态:500

春季休息方法:

@RequestMapping(value = "/users",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public ResponseEntity<User> createUser(@RequestBody User user) throws URISyntaxException {
    User result = userRepository.save(user);
    return ResponseEntity.created(new URI("/api/users/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("user", result.getId().toString()))
            .body(result);
}

春季域对象

@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractAuditingEntity implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotNull
    @Size(max = 100)
    @Column(length = 100, unique = true)
    private String login;
    @JsonIgnore
    @NotNull
    @Size(min = 5, max = 60)
    @Column(length = 60)
    private String password;
    @Size(max = 50)
    @Column(name = "first_name", length = 50)
    private String firstName;
    @Size(max = 50)
    @Column(name = "last_name", length = 50)
    private String lastName;
    @Email
    @Size(max = 100)
    @Column(length = 100, unique = true)
    private String email;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "user_tag",
        joinColumns = @JoinColumn(name = "users_id", referencedColumnName =     "ID"),
    inverseJoinColumns = @JoinColumn(name = "tags_id", referencedColumnName = "ID"))
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private List<Tag> tags = new ArrayList<>();
}

angular2域对象

export class User {
   id: number;
   login: string;
   password: string;
   firstName: string;
   lastName: string;
   email: string;
}

问题在于在密码字段上使用 @JsonIgnore,这使得在从阅读或写入JSON消息的读取或写入JSON消息时,一种解决方案是将@JsonPropertyaccess = Access.WRITE_ONLY一起使用,如下所示

public class User extends AbstractAuditingEntity implements Serializable {
  // Other fileds
  @NotNull
  @JsonProperty(access = Access.WRITE_ONLY)
  @Size(min = 5, max = 60)
  @Column(length = 60)
  private String password;
}

另一个解决方案是为密码字段实现一个getter和setter,并用@JsonIgnore和使用@JsonProperty的setter注释getter,如下所示

public class User extends AbstractAuditingEntity implements Serializable {
  // Other fileds
  @NotNull
  @Size(min = 5, max = 60)
  @Column(length = 60)
  private String password;
  @JsonIgnore
  public String getPassword() {
    return password;
  }
  @JsonProperty
  public void setPassword(String password) {
    this.password = password;
  }
}

最新更新