程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使用Spring Boot Data Rest在同一请求中保存许多对象大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使用Spring Boot Data Rest在同一请求中保存许多对象?

开发过程中遇到如何使用Spring Boot Data Rest在同一请求中保存许多对象的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使用Spring Boot Data Rest在同一请求中保存许多对象的解决方法建议,希望对你解决如何使用Spring Boot Data Rest在同一请求中保存许多对象有所启发或帮助;

错误的是,它试图读取请求正文Item,而实际上是多个Items

我相信您在这里有两个选择。在这种情况下,我通常要做的是创建另一个资源,例如ItemCollection,包装多个Items。然后,您可以使用标准的REST功能来ItemCollection处理,这实际上可以处理多个Items

您的第二个选择是手动替代一种方法来处理多个项目:http : //docs.spring.io/spring- data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr- response-处理程序

解决方法

我尝试使用POST方法保存实体的数组,并为其余资源传递数组,但是出现错误:

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]

当我向一个对象发送数据时,数据保存得很好!


我的实体:

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

我的资料库:

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

}

数据:

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

我试使用COntent-Type application / json并与text / uri-list一起使用。怎么了?我还有其他设置吗?

大佬总结

以上是大佬教程为你收集整理的如何使用Spring Boot Data Rest在同一请求中保存许多对象全部内容,希望文章能够帮你解决如何使用Spring Boot Data Rest在同一请求中保存许多对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。