gpt4 book ai didi

java - JsonMappingException : (was NullPointerException) through reference chain: Request ["requestId"]

转载 作者:行者123 更新时间:2023-12-01 09:30:34 29 4
gpt4 key购买 nike

我在放入数据库的新创建对象的 id 上收到此空指针异常,即使它应该是自动生成的。

@Entity
@Table(name = "REQUEST")
@EntityListeners(DatabaseListener.class)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "requestId", scope = Request.class)
public class Request implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "REQUEST_ID", nullable = false)
private Long requestId;

@Column(name = "REQUEST_NAME")
private String name;

@Column(name = "CATEGORY")
private String category;

@Column(name = "REQUEST_MESSAGE")
private String requestMessage;

@Column(name = "CREATED_ON")
private Date createdOn;

@ManyToOne
private User createdBy;

@ManyToOne
private User assignedTo;

@ManyToOne
private Status status;

@OneToMany(mappedBy = "request", fetch = FetchType.EAGER)
private List<Response> replies;

我正在调用的端点...

@RequestMapping(value = "/requests/addRequest", method = RequestMethod.POST)
public ResponseEntity<Request> addRequest(@RequestBody final Request request) {
if(request.getStatus() == null){
request.setStatus(request.getAssignedTo() == null ? statusRepo.findByType(StatusType.UNASSIGNED) : statusRepo.findByType(StatusType.ASSIGNED));
}

final Request saved = requestRepo.save(request);

return new ResponseEntity<>(saved, HttpStatus.CREATED);
}

如果需要,我也可以添加更多其他类。但我不明白为什么 requestId 将为空。我从应用程序调用该 enpoint 并向其传递一个 Request 对象,因此显然它是 null,但当它进入数据库时​​它不应该是 null。正确的?

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: (was java.lang.NullPointerException) (through reference chain: Request["requestId"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: Request["requestId"])
...
Caused by: com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain:Request["requestId"])
...
Caused by: java.lang.NullPointerException: null
at com.fasterxml.jackson.annotation.ObjectIdGenerator$IdKey.<init>(ObjectIdGenerator.java:125) ~[jackson-annotations-2.4.0.jar:2.4.0]

编辑:需要明确的是,我知道什么是空指针异常,我知道 requestId 为空并且这导致了问题,所以我不认为这是一个重复的问题。我不明白的是为什么它是空的。它应该是自动生成的,但正如 Jason 指出的,我可能必须首先确定 id 应用程序端。

最佳答案

不是最优雅的,但迄今为止是我能找到的唯一解决方案。

我创建了一个新端点,该端点创建一个空白Request,将其保存在数据库中并将其返回给应用程序。这样,应用程序现在就有了一个带有真实且正确 ID 的空白 Request

然后在我的 addRequest 端点中,我对该 id 进行查询,而不是创建新请求,而是更新现有请求。

编辑:实际上 addRequest 方法将保持不变,因为数据库只会更新现有条目。

关于java - JsonMappingException : (was NullPointerException) through reference chain: Request ["requestId"],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39453059/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com