gpt4 book ai didi

json - 无法反序列化模型实例。*来自START_ARRAY token \n,位于

转载 作者:行者123 更新时间:2023-12-05 00:17:20 24 4
gpt4 key购买 nike

我进行了REST服务迁移,现在尝试添加新项。

型号留言:

@Entity
@Table(name="message")
public class Message{

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private long id;
@Column(name = "MESSAGE")
private String message;
@Column(name = "AUTHOR")
private String author;
@Column(name = "CREATED")
@Temporal(TemporalType.DATE)
private Date created;

public Message() {}

public Message(Long id, String message, String author) {
this.id = id;
this.message = message;
this.author = author;
this.created = new Date();
}
+ getters / setters

MessageController:
@RestController
public class MessageController {

@Autowired
private MessageRepository messageRepository;

@RequestMapping(
value = "/api/messages",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> addMessage(@RequestBody Message newMessage) {
return new ResponseEntity<>(messageRepository.save(newMessage), HttpStatus.CREATED);
}
}

错误:

2016-11-06 10:52:53.857 WARN 1100 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of com.sttech.springrest.model.Message out of START_ARRAY token at [Source: java.io.PushbackInputStream@6ccdce8a; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.sttech.springrest.model.Message out of START_ARRAY token at [Source: java.io.PushbackInputStream@6ccdce8a; line: 1, column: 1]



通过 postman ,我在json中发送新数据并获得 postman 答复
[
{
"message": "Hello World1",
"author": "ABC"
}
]

"exception": "org.springframework.http.converter.HttpMessageNotReadableException", "message": "Could not read document: Can not deserialize instance of com.sttech.springrest.model.Message out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@6ccdce8a; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.sttech.springrest.model.Message out of START_ARRAY token\n at [Source: java.io.PushbackInputStream@6ccdce8a; line: 1, column: 1]"



如何解决?我认为模型没有ID。

最佳答案

您的服务需要一条消息。但是,您发送了一组消息(尽管它只包含一条消息)。

因此,而不是:

[
{
"message": "Hello World1",
"author": "ABC"
}
]

您应该发送:
{
"message": "Hello World1",
"author": "ABC"
}

关于json - 无法反序列化模型实例。*来自START_ARRAY token \n,位于,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40461647/

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