मैं Person
इकाई में एक पता सूची जोड़कर उदाहरण Accessing JPA Data with REST का विस्तार करना चाहता था।वसंत जेपीए रीस्ट वन से कई
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Address> addresses = new ArrayList<>();
// get and set methods...
}
Address
वर्ग एक बहुत ही सरल है: तो, मैं @OneToMany
टिप्पणी के साथ एक सूची addresses
जोड़ा
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String street;
private String number;
// get and set methods...
}
और अंत में मैं AddressRepository
इंटरफ़ेस कहा: तो फिर
public interface AddressRepository extends PagingAndSortingRepository<Address, Long> {}
मैंने कुछ पते वाले व्यक्ति को पोस्ट करने का प्रयास किया:
curl -i -X POST -H "Content-Type:application/json" -d '{ "firstName" : "Frodo", "lastName" : "Baggins", "addresses": [{"street": "somewhere", "number": 1},{"street": "anywhere", "number": 0}]}' http://localhost:8080/people
त्रुटि मैं मिलता है:
Could not read document: Failed to convert from type [java.net.URI] to type [ws.model.Address] for value 'street';
nested exception is java.lang.IllegalArgumentException: Cannot resolve URI street. Is it local or remote? Only local URIs are resolvable. (through reference chain: ws.model.Person[\"addresses\"]->java.util.ArrayList[1]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException: Failed to convert from type [java.net.URI] to type [ws.model.Address] for value 'street'; nested exception is java.lang.IllegalArgumentException: Cannot resolve URI street. Is it local or remote? Only local URIs are resolvable. (through reference chain: ws.model.Person[\"addresses\"]->java.util.ArrayList[1])
उचित विधि कई रिश्ते और पोस्ट json वस्तुओं उन्हें करने के लिए कई लोगों के लिए एक और कई बनाने के लिए है कौन?
आपने हमें अपने ओआरएम के लिए इकाई कक्षाएं दिखायी हैं, लेकिन आपने हमें कुछ भी नहीं दिखाया है जो आरईएसटी के लिए एनोटेटेड है। – scottb
एक कस्टम कनवर्टर का उपयोग करने का सुझाव देने वाला यह जवाब देखें http://stackoverflow.com/questions/24781516/spring-data-rest-field-converter – dseibert
@scottb मैं दोनों रिपॉजिटरीज़ के लिए ट्यूटोरियल में '@ रिपोजिटरी रीस्ट रिसोर्स 'एनोटेशन का उपयोग करता हूं (व्यक्ति, पता)। यह संस्थाओं के लिए सामान्य आरईएसटी अंतराल बनाता है। –