不能 post 具有一对一 HAL 的实体 link 使用 Spring 剩余数据 mvc
Can't post entity with one to one HAL link using Spring rest data mvc
我尝试保存 Clinic 实体 nullable = false
与 User 实体的 OneToOne nullable = false
关系。
诊所实体:
//....Some fields
@OneToOne(optional=false,fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@NotNull
private User user;
//.. Getters & Setters
用户实体
//....Some fields
@OneToOne(fetch = FetchType.LAZY,mappedBy="user",cascade=CascadeType.ALL,orphanRemoval = true)
private Clinic clinic;
//.. Getters & Setters
Post用户成功
curl -i -X POST -H "Content-Type: application/json" -d '{"firstName" : "Khaled","lastName" : "Lela","userName" : "KhaledLela","password" : "128ecf542a35ac5270a87dc740918404","email" : "example@gmail.com", "phone" : "12345678","gender" : "MALE", "level" : "ADMIN"}' http://localhost:8080/clinicfinder/api/user
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/clinicfinder/api/user/4
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Apr 2017 12:26:41 GMT
{
"token" : null,
"firstName" : "Khaled",
"lastName" : "Lela",
"userName" : "KhaledLela",
"password" : "128ecf542a35ac5270a87dc740918404",
"email" : "example@gmail.com",
"phone" : "12345678",
"gender" : "MALE",
"level" : "ADMIN",
"birthDate" : null,
"createDate" : null,
"updateDate" : null,
"_links" : {
"self" : {
"href" : "http://localhost:8080/clinicfinder/api/user/4"
},
"user" : {
"href" : "http://localhost:8080/clinicfinder/api/user/4"
},
"clinic" : {
"href" : "http://localhost:8080/clinicfinder/api/user/4/clinic"
}
}
}
不能Post与用户link
一起诊所
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"clinc","address":"address","city":"city","area":"area","user":"http://localhost:8080/clinicfinder/api/user/2"}' http://localhost:8080/clinicfinder/api/clinic
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=user, rootBeanClass=class com.domain.entity.Clinic, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]
诊所资料库
@RepositoryRestResource(path = "clinic")
public interface ClinicRepo extends CrudRepository<Clinic, Long> {}
Pom
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<spring.data.jpa.version>1.11.1.RELEASE</spring.data.jpa.version>
<spring.data.rest.webmvc.version>2.6.1.RELEASE</spring.data.rest.webmvc.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- Spring Rest Repository -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.jpa.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>${spring.data.rest.webmvc.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
感谢 @Alan Hay,我的错是我错过了在 Clinic 实体上添加 setUser() & getUser()
。
添加它们后效果非常好,..
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"clinc","address":"address","city":"city","area":"area","user":"http://localhost:8080/clinicfinder/api/user/2"}' http://localhost:8080/clinicfinder/api/clinic
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/clinicfinder/api/clinic/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Apr 2017 15:59:16 GMT
{
"name" : "clinc",
"address" : "address",
"city" : "city",
"area" : "area",
"longitude" : null,
"latitude" : null,
"createDate" : null,
"updateDate" : null,
"doctors" : [ ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/clinicfinder/api/clinic/1"
},
"clinic" : {
"href" : "http://localhost:8080/clinicfinder/api/clinic/1"
},
"user" : {
"href" : "http://localhost:8080/clinicfinder/api/clinic/1/user"
}
}
}
我尝试保存 Clinic 实体 nullable = false
与 User 实体的 OneToOne nullable = false
关系。
诊所实体:
//....Some fields
@OneToOne(optional=false,fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
@NotNull
private User user;
//.. Getters & Setters
用户实体
//....Some fields
@OneToOne(fetch = FetchType.LAZY,mappedBy="user",cascade=CascadeType.ALL,orphanRemoval = true)
private Clinic clinic;
//.. Getters & Setters
Post用户成功
curl -i -X POST -H "Content-Type: application/json" -d '{"firstName" : "Khaled","lastName" : "Lela","userName" : "KhaledLela","password" : "128ecf542a35ac5270a87dc740918404","email" : "example@gmail.com", "phone" : "12345678","gender" : "MALE", "level" : "ADMIN"}' http://localhost:8080/clinicfinder/api/user HTTP/1.1 201 Created Server: Apache-Coyote/1.1 Location: http://localhost:8080/clinicfinder/api/user/4 Content-Type: application/hal+json;charset=UTF-8 Transfer-Encoding: chunked Date: Thu, 20 Apr 2017 12:26:41 GMT { "token" : null, "firstName" : "Khaled", "lastName" : "Lela", "userName" : "KhaledLela", "password" : "128ecf542a35ac5270a87dc740918404", "email" : "example@gmail.com", "phone" : "12345678", "gender" : "MALE", "level" : "ADMIN", "birthDate" : null, "createDate" : null, "updateDate" : null, "_links" : { "self" : { "href" : "http://localhost:8080/clinicfinder/api/user/4" }, "user" : { "href" : "http://localhost:8080/clinicfinder/api/user/4" }, "clinic" : { "href" : "http://localhost:8080/clinicfinder/api/user/4/clinic" } } }
不能Post与用户link
一起诊所curl -i -X POST -H "Content-Type: application/json" -d '{"name":"clinc","address":"address","city":"city","area":"area","user":"http://localhost:8080/clinicfinder/api/user/2"}' http://localhost:8080/clinicfinder/api/clinic
List of constraint violations:[ ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=user, rootBeanClass=class com.domain.entity.Clinic, messageTemplate='{javax.validation.constraints.NotNull.message}'} ]
诊所资料库
@RepositoryRestResource(path = "clinic") public interface ClinicRepo extends CrudRepository<Clinic, Long> {}
Pom
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.6</maven.compiler.source> <maven.compiler.target>1.6</maven.compiler.target> <spring.data.jpa.version>1.11.1.RELEASE</spring.data.jpa.version> <spring.data.rest.webmvc.version>2.6.1.RELEASE</spring.data.rest.webmvc.version> </properties> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.41</version> <scope>runtime</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- Spring Rest Repository --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>${spring.data.jpa.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-rest-webmvc</artifactId> <version>${spring.data.rest.webmvc.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency>
感谢 @Alan Hay,我的错是我错过了在 Clinic 实体上添加 setUser() & getUser()
。
添加它们后效果非常好,..
curl -i -X POST -H "Content-Type: application/json" -d '{"name":"clinc","address":"address","city":"city","area":"area","user":"http://localhost:8080/clinicfinder/api/user/2"}' http://localhost:8080/clinicfinder/api/clinic
HTTP/1.1 201 Created
Server: Apache-Coyote/1.1
Location: http://localhost:8080/clinicfinder/api/clinic/1
Content-Type: application/hal+json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 20 Apr 2017 15:59:16 GMT
{
"name" : "clinc",
"address" : "address",
"city" : "city",
"area" : "area",
"longitude" : null,
"latitude" : null,
"createDate" : null,
"updateDate" : null,
"doctors" : [ ],
"_links" : {
"self" : {
"href" : "http://localhost:8080/clinicfinder/api/clinic/1"
},
"clinic" : {
"href" : "http://localhost:8080/clinicfinder/api/clinic/1"
},
"user" : {
"href" : "http://localhost:8080/clinicfinder/api/clinic/1/user"
}
}
}