1

I'm try save a array of an Entity using POST method passing an array for rest resource, but I have an error:

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of br.com.servtech.almox.model.Item out of START_ARRAY token
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2af1e451; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of br.com.servtech.almox.model.Item out of START_ARRAY token
 at [Source: org.apache.catalina.connector.CoyoteInputStream@2af1e451; line: 1, column: 1]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:228) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readInternal(AbstractJackson2HttpMessageConverter.java:205) ~[spring-web-4.3.3.RELEASE.jar:4.3.3.RELEASE]

When's I send one object the data, the data is saved very well!


My Entity:

@Entity
public class Item implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Basic
    private String name;

    @Basic
    private Integer quantity;

    @Basic
    private Double cash;

    @ManyToOne
    private Requirement requirement;

    //getters and setters
}

My Repository:

@RepositoryRestResource
@CrossOrigin
public interface ItemDAO extends CrudRepository<Item, Long> {

}

The Data:

[{ 
    "name": "A1", 
    "quantity": 3, 
    "cash": 5.80
}, { 
    "name": "B2", 
    "quantity": 3, 
    "cash": 5.80
}]

I've tried to using the Content-Type application/json and using with text/uri-list. What is wrong? I do some more setup?

3
  • Please also post your controller method and the associated value object that comes with the request call.. Commented Nov 1, 2016 at 15:42
  • He is using Spring Data Rest, he has already done that. Commented Nov 1, 2016 at 15:51
  • 2
    I've requested the feature, please vote: jira.spring.io/browse/DATAREST-1285 Commented Sep 11, 2018 at 8:03

1 Answer 1

2

What is wrong is that is trying to read your request body as an Item, when it is in fact multiple Items.

I believe you have two choices here. What I would normally do in this situation would be to create another resource, such as ItemCollection, to wrap multiple Items. Then you could have standard REST functionality to take in an ItemCollection, which would essentially handle multiple Items.

Your second option would be to manually override a method to handle multiple itmes: http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I ended up following your suggestion of the documentation. I leave here the link of the test repository that I made => github.com/rafaeldev/spring-boot-tests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.