Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not deserialize instance of model.* out of START_ARRAY token\n at

I make traning REST service and now I try to add new item.

model Message:

@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);
    }
}

Error:

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]

Through Postman I send new data in json and get Postman answer

[
  {
    "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]"

How to fix it? I think the model does't get id.

like image 882
Sergei R Avatar asked Nov 07 '16 09:11

Sergei R


1 Answers

Your service expects a single message. However, you send an array of messages (though it only contains a single message).

So instead of:

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

you should just send:

{
  "message": "Hello World1",
  "author": "ABC"
}
like image 81
Codo Avatar answered Nov 15 '22 05:11

Codo