我编写了一个包含两个实体的小型SpringBoot应用程序。它们如下所示,与电话具有OneToMany关系的人:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Person {
@OneToMany(mappedBy="person")
private Set<Phone> phones = new HashSet<>();
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String firstName;
private String lastName;
@Column(columnDefinition="int default 0")
private Integer age;
public Person() {
super();
}
public Person(String firstName, String lastName, Integer age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public long getId() {
return id;
}
public Set<Phone> getPhones() {
return phones;
}
public void setPhones(Set<Phone> phones) {
this.phones = phones;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Phone实体有ManyToOne:
package org.prdas;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Phone {
@ManyToOne
private Person person;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String homePhone;
private String workPhone;
public Phone() {
}
public Phone(Person person, String homePhone, String workPhone) {
this.person = person;
this.homePhone = homePhone;
this.workPhone = workPhone;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
public String getWorkPhone() {
return workPhone;
}
public void setWorkPhone(String workPhone) {
this.workPhone = workPhone;
}
public Long getId() {
return id;
}
}
我已经定义了以下两个存储库:
import java.util.List;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {
List<Person> findByLastName(@Param("name") String name);
List<Person> findByFirstName(@Param("name") String name);
}
import java.util.Collection;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource(collectionResourceRel = "{id}", path = "phone")
public interface PhoneRepository extends JpaRepository<Phone, Long> {
Collection<Phone> findByPersonLastName(@Param("name") String name);
}
最后是Application类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
现在,当我使用Postman运行jar时,我可以使用URL来获取,发布,删除和删除Person实体,例如,
(POST) http://localhost:8080/people/
体
{"firstName":"Jack", "lastName":"Brown"}
但是,当我尝试在以下人员的情况下为手机发布POST时:
(Post) http://localhost:8080/people/5/phones
体
{"homePhone":"24102234", "workPhone":"9831299878"}
我得到的答复如下:
{
"timestamp": 1443796964061,
"status": 405,
"error": "Method Not Allowed",
"exception": "org.springframework.web.HttpRequestMethodNotSupportedException",
"message": "Request method 'POST' not supported",
"path": "/people/5/phones"
}
如果有人能帮我解决这个问题,我将不胜感激。
谢谢, 普拉纳