简体   繁体   中英

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. 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. The Jason contains 3 main Elements:

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.

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.

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.

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

{
"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.

You can (and should) however specify the JSON property name using the following annotation:

 @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. Usually one uses ignoreUnknown = true , but then you should add tests to check if the data really is properly marshalled to JSON and back.

Side note:

  • Using entities as arguments or result types for web services is bad practice.
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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