java JSON返回空内容
我有个问题, 我的JSON不会返回应该通过OneToMany关系返回给我的内容。 它应该将Person对象的详细信息返回给我,而不是返回一个空数组。 下面是我所有的代码
人。爪哇
@Entity
@Table(name = "person_table")
public class Person implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "person_id")
private Long id;
@Column(name = "person_name")
private String name;
@Column(name = "person_lastname")
private String lastname;
@Column(name = "person_details")
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "person_id")
private List<PersonDetails> details = new ArrayList<>();
Person(){}
//getters, setters
个人信息。爪哇
@Entity
@Table(name = "person_details")
public class PersonDetails implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id_details")
private Long id;
@Column(name = "age_person")
private int age;
private String city;
private String country;
private int phoneNumber;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "details_id")
private Person person;
PersonDetails(){}
//getters,setters
人格的。爪哇
@Repository
public interface PersonRepository extends JpaRepository<Person, Long>{
}
个人主义。爪哇
@Repository
public interface PersonDetailsRepository extends JpaRepository<PersonDetails, Long> {
List<PersonDetails> getByPersonId(Long id);
}
人事管理员。爪哇
@RestController
@RequestMapping("/api/persons")
public class PersonDetailsController {
PersonRepository personRepository;
PersonDetailsRepository personDetailsRepository;
@Autowired
public PersonDetailsController(PersonDetailsRepository personDetailsRepository, PersonRepository personRepository) {
this.personDetailsRepository = personDetailsRepository;
this.personRepository = personRepository;
}
@GetMapping("/{id}/details")
List<PersonDetails> getDetails(@PathVariable Long id){
return personDetailsRepository.getByPersonId(id);
}
}
我跳过人事管理员。爪哇
当我转到uri:localhost:8080/api/persons/1/details时我的JSON:
[]
共 (0) 个答案