【问题标题】:error com.fasterxml.jackson.databind.exc.MismatchedInputException Spring boot错误 com.fasterxml.jackson.databind.exc.MismatchedInputException 春季启动
【发布时间】:2018-12-27 15:46:20
【问题描述】:

我有一个控制器,我在其中定义了一个 PostMapping 方法来创建这样的新广告:

@PostMapping
public ResponseEntity<?> registerAd(@RequestBody DonationAd ad){
    repository.save(ad);
    return ResponseEntity.accepted().body(new ApiResponse(true,"sucess"));
}

当我尝试像这样在邮递员上使用 raw 发出请求时:

{
"title":"lalal",
"description":"lalallalalal",
"user":2,
"images":[8,17],
"state":"BAD",
"adress":"textextextetextetxt"
}

在创建用户实例时出现问题时,我收到了此回复:

{
"timestamp": "2018-12-27T14:51:30.465+0000",
"status": 400,
"error": "Bad Request",
"message": "JSON parse error: Cannot construct instance of `com.betroc.model.User` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (2); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.betroc.model.User` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (2)\n at [Source: (PushbackInputStream); line: 4, column: 8] (through reference chain: com.betroc.model.DonationAd[\"user\"])",
"path": "/api/donationAds"}

知道同样的方法也可以使用 postMan 完美地处理表单数据

这是用户类:

public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank
@Size(max = 40)
private String name;

@NotBlank
@Size(max = 15)
private String username;

@NaturalId
@NotBlank
@Size(max = 40)
@Email
private String email;

@JsonIgnore
@NotBlank
@Size(max = 100)
private String password;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "user_roles",
        joinColumns = @JoinColumn(name = "user_id"),
        inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles = new HashSet<>();

private boolean enabled;

public User() {
}

public User(String name, String username, String email, String password) {
    this.name = name;
    this.username = username;
    this.password = password;
    this.email = email;
    this.enabled = false;

}
//getters ans setters
}

这是扩展抽象类广告的DoantionAd:

public class DonationAd extends Advertisement{

@Enumerated(EnumType.STRING)
private State state;

@Column(columnDefinition = "TEXT")
private String address;

//getters and setters
}

然后是广告:

public abstract class Advertisement {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@NotNull
private String title ;

@NotNull
@Column(columnDefinition = "TEXT")
private String description;

@Temporal(TemporalType.TIMESTAMP)
private Date creationDate;

@Temporal(TemporalType.TIMESTAMP)
private Date modificationDate;

@ManyToOne
private User  user;


@OneToMany(cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<Image> images;

//getters and setters
}

【问题讨论】:

  • 用户是广告中的一个对象,所以我认为您必须设置孔对象,其他认为我看不到任何地方 @OneToMany(mappedBy="user",cascade=CascadeType.ALL)私人列表 ads=new ...
  • 问题是使用表单数据而不是原始数据时相同的代码工作,然后我认为spring可以将id映射到相应的对象。有@ManyToOne私人用户用户;在广告中

标签: json spring-boot jackson


【解决方案1】:

如果您尝试使用此 json:

{
   "title":"lalal",
   "description":"lalallalalal",
   "user":{  
      "id":2
   },
   "state":"BAD",
   "address":"textextextetextetxt"
}

你可以添加其他属性

【讨论】:

    猜你喜欢
    • 2017-10-10
    • 2017-11-05
    • 1970-01-01
    • 2014-08-12
    • 2020-05-12
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    相关资源
    最近更新 更多