简体   繁体   English

SpringBoot ArrayList/JSON:将某种类型的所有元素添加到该类型的列表中

[英]SpringBoot ArrayList/JSON: add all Elements of a certain type to a list of this type

I am trying to write a REST API to manage Invoices.我正在尝试编写一个 REST API 来管理发票。 As a part of this project I encounter a problem.作为这个项目的一部分,我遇到了一个问题。 I can't figure out how to add Objects of a certain type to a List of that type if the Objects are specified in a Json post request.如果在 Json 发布请求中指定了对象,我无法弄清楚如何将某种类型的对象添加到该类型的列表中。 The Jason contains 3 main Elements: Jason 包含 3 个主要元素:

An invoice number - A recipient (with all data required for that recipient) - an unknown number of articles发票编号 - 收件人(包含该收件人所需的所有数据) - 未知数量的物品

and while the Controller saves the Invoice number and the recipient just fine, it doesn't save the Articles nor does it add those to the Invoice list.虽然控制器保存发票编号和收件人就好了,但它不会保存文章,也不会将它们添加到发票列表中。

I did search Stackoverflow for this type of problem but, until now I did not find a solution.我确实在 Stackoverflow 上搜索过此类问题,但是直到现在我还没有找到解决方案。

I also tried mapping the entire Jason Post to a map, using ObjectMapper but that is neither very hand, nor efficient if I have to map every single Input to a Map, and then search the Map in order to rebuild the Objects so I can save them to the database.我还尝试使用 ObjectMapper 将整个 Jason Post 映射到地图,但如果我必须将每个输入映射到地图,然后搜索地图以重建对象以便我可以保存,这既不方便,也不高效他们到数据库。

With me having had absolutely no knowledge of Spring boot and only very basic knowledge of Java, prior to starting my work on this project, I now find myself stuck.由于我对 Spring boot 一无所知,只了解 Java 的基本知识,因此在开始这个项目的工作之前,我发现自己被困住了。

In order to give you an Idea here is my controller:为了给你一个想法,这里是我的控制器:

@RestController
public class RechnungController {

@Autowired
private RechnungRepository rechnung_repo;

@Autowired
private EmpfaengerRepository empf_repo;



//Get Mappings


@PostMapping ("/rechnungen")
@ResponseBody
public Rechnung rechnungSave (@RequestBody Rechnung r) {

     Rechnung r_save = rechnung_repo.save(r);
     return r_save;
}



//More get Mapping


}

Here is my Invoice definition:这是我的发票定义:

@Entity
public class Rechnung  {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long Id;

    private int rechnungsnummer;

    @OneToMany (
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    @JoinColumn (name ="rechnungID")
    private List <Artikel> artikelliste = new ArrayList <Artikel>();//erstellt eine Liste von Artikeln vom Typ ArrayList


    @OneToOne (cascade = CascadeType.ALL)
    @JoinColumn(name= "empf", referencedColumnName = "Id")
    @RestResource
    private Empfaenger empfaenger;

//Getters Setters and Constructor

and here is the Json i would like to post这是我想发布的 Json

{
"rechnungsnummer":"02552425",
"empfaenger": {
    "name" : "nnnnnn",
    "vorname" : "vvvvv"
    },

"artikels":{
    "artikel":{

        "preis":"12.33"
    }
"artikel":{

        "preis":"12.33"
    }
}
}

Any pointers woul be helpfull.任何指针都会有所帮助。 Thank you.谢谢你。

The JSON property name is by default the Java property name, so it would be artikelliste in the JSON.默认情况下,JSON 属性名称是 Java 属性名称,因此它在 JSON 中将是artikelliste

You can (and should) however specify the JSON property name using the following annotation:但是,您可以(并且应该)使用以下注释指定 JSON 属性名称:

 @JsonProperty("artikels")
 private List <Artikel> artikelliste ...

The @JsonIgnoreProperties can help you spot such errors - in strict mode ( ignoreUnknown = false ), using unknown properties will cause an error. @JsonIgnoreProperties可以帮助您发现此类错误 - 在严格模式下( ignoreUnknown = false ),使用未知属性会导致错误。 Usually one uses ignoreUnknown = true , but then you should add tests to check if the data really is properly marshalled to JSON and back.通常使用ignoreUnknown = true ,但是你应该添加测试来检查数据是否真的正确地编组到 JSON 并返回。

Side note:边注:

  • Using entities as arguments or result types for web services is bad practice.使用实体作为 Web 服务的参数或结果类型是不好的做法。
  • You should use a DTO (data transfer object) instead, where you can control what you expose or accept, and map between the DTOs and entities.您应该改用 DTO(数据传输对象),您可以在其中控制公开或接受的内容,并在 DTO 和实体之间进行映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM