【问题标题】:Can't post entity with one to one HAL link using Spring rest data mvc无法使用 Spring REST 数据 mvc 发布具有一对一 HAL 链接的实体
【发布时间】:2017-09-17 03:09:30
【问题描述】:

我尝试保存 Clinic 实体与 OneToOne nullable = false 关系与 User 实体。

诊所实体:

//....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
  • 发布用户成功。

     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"
    }
      }
    }
    
  • 无法通过用户链接发布诊所

    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
    

约束违规列表:[ 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>
    

【问题讨论】:

  • 你在诊所里肯定有setUser()(或getUser())方法?
  • @AlanHay OMG,IDE setter&getter 生成错过了setUser() &amp; getUser()) 可能是我在生成 getter&setter 后添加了这个属性。添加它们后它现在可以工作了。感谢您的帮助,真的节省了我的时间。

标签: jpa spring-data spring-data-jpa spring-data-rest spring-hateoas


【解决方案1】:

感谢 @Alan Hay,我错过了在 Clinic 实体上添加 setUser() &amp; 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"
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-09
    • 2017-11-13
    • 2014-07-09
    • 2017-08-21
    • 2014-05-05
    • 2017-12-10
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多