我正在使用JHipster,并且正在运行以下curl命令:
POST / api / gang-users
连身:
{
"role": "member",
"gang": "3",
"user": "1"
}
但出现以下错误:
JSON解析错误:无法构造的实例
com.getgreetapp.greetapp.domain.User
(尽管至少有一位创建者 存在):没有要反序列化的String-argument构造函数/工厂方法 从字符串值('1');嵌套异常为 com.fasterxml.jackson.databind.exc.MismatchedInputException:无法 构造com.getgreetapp.greetapp.domain.User
的实例(尽管 至少存在一个创建者):没有字符串参数构造函数/工厂 [源:]从字符串值('1')\ n反序列化的方法 (PushbackInputStream);第4行,第10列](通过参考链: com.getgreetapp.greetapp.domain.GangUser [\“用户\”])
GangUserResource createGangUser
@PostMapping("/gang-users")
@Timed
public ResponseEntity<GangUser> createGangUser(@Valid @RequestBody GangUser gangUser) throws URISyntaxException {
log.debug("REST request to save GangUser : {}", gangUser);
if (gangUser.getId() != null) {
throw new BadRequestAlertException("A new gangUser cannot already have an ID", ENTITY_NAME, "idexists");
}
User user = userRepository.findById(gangUser.getUser());
GangUser result = gangUserRepository.save(gangUser);
return ResponseEntity.created(new URI("/api/gang-users/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
.body(result);
}
GangUser
package com.getgreetapp.greetapp.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.util.Objects;
/**
* A GangUser.
*/
@Entity
@Table(name = "gang_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class GangUser implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(name = "jhi_role", nullable = false)
private String role;
@ManyToOne
private Gang gang;
@ManyToOne
private User user;
public GangUser() {
}
public GangUser(String role, Gang gang, User user)
{
this.role = role;
this.gang = gang;
this.user = user;
}
// jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRole() {
return role;
}
public GangUser role(String role) {
this.role = role;
return this;
}
public void setRole(String role) {
this.role = role;
}
public Gang getGang() {
return gang;
}
public GangUser gang(Gang gang) {
this.gang = gang;
return this;
}
public void setGang(Gang gang) {
this.gang = gang;
}
public User getUser() {
return user;
}
public GangUser user(User user) {
this.user = user;
return this;
}
public void setUser(User user) {
this.user = user;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GangUser gangUser = (GangUser) o;
if (gangUser.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), gangUser.getId());
}
@Override
public int hashCode() {
return Objects.hashCode(getId());
}
@Override
public String toString() {
return "GangUser{" +
"id=" + getId() +
", role='" + getRole() + "'" +
"}";
}
}
用户
package com.getgreetapp.greetapp.domain;
import com.getgreetapp.greetapp.config.Constants;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.validation.constraints.Email;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.time.Instant;
/**
* A user.
*/
@Entity
@Table(name = "jhi_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
@Column(length = 50, unique = true, nullable = false)
private String login;
@JsonIgnore
@NotNull
@Size(min = 60, max = 60)
@Column(name = "password_hash", length = 60, nullable = false)
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(min = 5, max = 254)
@Column(length = 254, unique = true)
private String email;
@NotNull
@Column(nullable = false)
private boolean activated = false;
@Size(min = 2, max = 6)
@Column(name = "lang_key", length = 6)
private String langKey;
@Size(max = 256)
@Column(name = "image_url", length = 256)
private String imageUrl;
@Size(max = 20)
@Column(name = "activation_key", length = 20)
@JsonIgnore
private String activationKey;
@Size(max = 20)
@Column(name = "reset_key", length = 20)
@JsonIgnore
private String resetKey;
@Column(name = "reset_date")
private Instant resetDate = null;
@JsonIgnore
@ManyToMany
@JoinTable(
name = "jhi_user_authority",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "name")})
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@BatchSize(size = 20)
private Set<Authority> authorities = new HashSet<>();
public User() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLogin() {
return login;
}
// Lowercase the login before saving it in database
public void setLogin(String login) {
this.login = StringUtils.lowerCase(login, Locale.ENGLISH);
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public boolean getActivated() {
return activated;
}
public void setActivated(boolean activated) {
this.activated = activated;
}
public String getActivationKey() {
return activationKey;
}
public void setActivationKey(String activationKey) {
this.activationKey = activationKey;
}
public String getResetKey() {
return resetKey;
}
public void setResetKey(String resetKey) {
this.resetKey = resetKey;
}
public Instant getResetDate() {
return resetDate;
}
public void setResetDate(Instant resetDate) {
this.resetDate = resetDate;
}
public String getLangKey() {
return langKey;
}
public void setLangKey(String langKey) {
this.langKey = langKey;
}
public Set<Authority> getAuthorities() {
return authorities;
}
public void setAuthorities(Set<Authority> authorities) {
this.authorities = authorities;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
return !(user.getId() == null || getId() == null) && Objects.equals(getId(), user.getId());
}
@Override
public int hashCode() {
return Objects.hashCode(getId());
}
@Override
public String toString() {
return "User{" +
"login='" + login + '\'' +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", imageUrl='" + imageUrl + '\'' +
", activated='" + activated + '\'' +
", langKey='" + langKey + '\'' +
", activationKey='" + activationKey + '\'' +
"}";
}
}
帮派
package com.getgreetapp.greetapp.domain;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import javax.persistence.*;
import javax.validation.constraints.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Objects;
/**
* A Gang.
*/
@Entity
@Table(name = "gang")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Gang implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotNull
@Column(name = "name", nullable = false)
private String name;
@NotNull
@Column(name = "description", nullable = false)
private String description;
@NotNull
@Column(name = "longitude", precision = 10, scale = 2, nullable = false)
private BigDecimal longitude;
@NotNull
@Column(name = "latitude", precision = 10, scale = 2, nullable = false)
private BigDecimal latitude;
@NotNull
@Column(name = "membership_approval", nullable = false)
private String membershipApproval;
@NotNull
@Column(name = "privacy", nullable = false)
private String privacy;
public Gang() {}
// jhipster-needle-entity-add-field - JHipster will add fields here, do not remove
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public Gang name(String name) {
this.name = name;
return this;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public Gang description(String description) {
this.description = description;
return this;
}
public void setDescription(String description) {
this.description = description;
}
public BigDecimal getLongitude() {
return longitude;
}
public Gang longitude(BigDecimal longitude) {
this.longitude = longitude;
return this;
}
public void setLongitude(BigDecimal longitude) {
this.longitude = longitude;
}
public BigDecimal getLatitude() {
return latitude;
}
public Gang latitude(BigDecimal latitude) {
this.latitude = latitude;
return this;
}
public void setLatitude(BigDecimal latitude) {
this.latitude = latitude;
}
public String getMembershipApproval() {
return membershipApproval;
}
public Gang membershipApproval(String membershipApproval) {
this.membershipApproval = membershipApproval;
return this;
}
public void setMembershipApproval(String membershipApproval) {
this.membershipApproval = membershipApproval;
}
public String getPrivacy() {
return privacy;
}
public Gang privacy(String privacy) {
this.privacy = privacy;
return this;
}
public void setPrivacy(String privacy) {
this.privacy = privacy;
}
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here, do not remove
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Gang gang = (Gang) o;
if (gang.getId() == null || getId() == null) {
return false;
}
return Objects.equals(getId(), gang.getId());
}
@Override
public int hashCode() {
return Objects.hashCode(getId());
}
@Override
public String toString() {
return "Gang{" +
"id=" + getId() +
", name='" + getName() + "'" +
", description='" + getDescription() + "'" +
", longitude=" + getLongitude() +
", latitude=" + getLatitude() +
", membershipApproval='" + getMembershipApproval() + "'" +
", privacy='" + getPrivacy() + "'" +
"}";
}
}
答案 0 :(得分:2)
在我的拙见中,有几件事需要检查。
首先,不建议使用相同的对象将数据存储在数据库中并响应请求。数据对象可以是GangUser,User和Gang,而响应对象GangUserApi,UserApi和GangApi。使用这种方法,您可以修改图层而无需修改其他图层。
第二,您的服务需要一个内部有用户的GangUser。用于将其存储在数据库中的同一用户对象。您只是发送一个字符串(“ 1”)而不是那里的对象。
一种快速的解决方案是修改JSON:
{
"role": "member",
"gang": "3",
"user": {
"id":"1"
}
}
但是我想知道您是否了解正在发生的事情。这比只给您一个快速解决方案更重要。
干杯。