无法从START_OBECT令牌中反序列化"java.lang.Long"的实例;在弹簧靴柱上



我想从我的angular应用程序向我的spring-boot后端发布一个新游戏。我从angular通过一个帖子来做这件事。这个游戏与我在spring-boot JPA中的用户实体有关系。但当我发帖时,我在控制台中收到一个400错误代码,上面写着:

"MethodArgumentNotValidException"fieldErrors":{"字段":"用户","错误代码":"NotNull"}]}

这是我的文章中的数据:

{category: "sport", sessieId: 20004, userID: 10004, score: null}

我的游戏controller:

@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@RequestMapping(value = "/api/games", produces = MediaType.APPLICATION_JSON_VALUE)
public class GameController {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Long createGame(@RequestBody @Valid final GameDTO gameDTO) {
return gameService.create(gameDTO);
}
// more methods for http
}

我的游戏domain:

@Entity
public class Game {
@Id
@Column(nullable = false, updatable = false)
@SequenceGenerator(name = "primary_sequence", sequenceName = "primary_sequence",
allocationSize = 1, initialValue = 10000)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "primary_sequence")
private Long id;
@Column(nullable = false)
private Integer sessieId;
@Column(nullable = false)
private String category;
@Column
private String score;
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

我的游戏DTO:

public class GameDTO {
private Long id;
@NotNull
private Integer sessieId;
@NotNull
@Size(max = 255)
private String category;
@Size(max = 255)
private String score;
@NotNull
private Long user;

我的游戏service:的创建方法

public Long create(final GameDTO gameDTO) {
final Game game = new Game();
mapToEntity(gameDTO, game);
return gameRepository.save(game).getId();
}

所以我看到了Game实体对用户的期望很长,所以我试图在游戏的帖子中发布对象用户。帖子主体:

{category: "sport", sessieId: 20004,…}
category: "sport"
score: null
sessieId: 20004
user: {id: 10004, firstName: "test5", lastName: "test5", email: "test5@test.nl", password: "test"}

但后来我得到了以下错误:

JSON解析错误:无法从START_OBECT令牌中反序列化java.lang.Long的实例;

我需要在帖子中传递用户对象吗?然后我如何修复错误?

类GameDTO中用户的tpye不正确。它是"长";类型,但";用户";在post-json中是一个对象。

public class GameDTO {
private Long id;
@NotNull
private Integer sessieId;
@NotNull
@Size(max = 255)
private String category;
@Size(max = 255)
private String score;
@NotNull
private User user;
}

最新更新