春天休息与角2

时间:2017-03-20 17:46:36

标签: spring spring-mvc post angular2-services spring-hateoas

我在使用Angular 2应用程序将新行插入数据库时​​遇到问题。

当我尝试在数据库中插入一个具有外键的新条目到数据库中的另一个表时,会出现问题。我在Spring中定义的两个模型是:

@Entity
public class A{

    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @OneToMany(mappedBy = "A")
    private List<B> b = new ArrayList<B>();
    ...
}

@Entity
public class B{

    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @NotNull
    @ManyToOne
    @JoinColumn(name="aId")
    private A a;
    ...
}

后端的这两个模型在前端有对应的模型。我添加了_links部分,因为Spring Rest api提供链接而不是外键:

export class A{
  id: number;

  b: B[];

  _links:{...}
  ...
}

export class B{
  id: number;

  a: A;

  _links:{...}
  ...
}

我根据从api获得的信息创建了这些模型。例如,localhost:8080 / api / b / 1上的get请求给出:

{
  "id" : 1,
  "_links" : {
    "self" : {
      "href" : "http://localhost:4200/api/b/1"
    },
    "b" : {
      "href" : "http://localhost:4200/api/b/1"
    },
    "a" : {
      "href" : "http://localhost:4200/api/b/1/a"
    }
  }
}

我可以轻松地将新行插入表A(因为它不包含外键),我的角度2服务方法如下所示:

  createA(a: A): Observable<A[]>{
    return this.http.post(this.AUrl, a)
                    .map((res:Response) => res.json())
                    .catch((error:any) => Observable.throw(error.json().error || 'Server Error'));
  }

同样,创建新模型B的服务方法如下:

  createB(b: B): Observable<B[]>{
    return this.http.post(this.BUrl, b)
                    .map((res:Response) => res.json())
                    .catch((error:any) => Observable.throw(error.json().error || 'Server Error'));
  }

我在Spring中的存储库定义为:

@RepositoryRestResource(collectionResourceRel="a", path="a", itemResourceRel="a")
public interface ARepository extends JpaRepository<A, Integer>{
}

@RepositoryRestResource(collectionResourceRel="b", path="b", itemResourceRel="b"))
public interface BRepository extends JpaRepository<B, Long>{
}

我在Spring中的存储库配置是:

  @Configuration
public class MyRestConfiguration extends RepositoryRestMvcConfiguration{

    @Override
    protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config){
        config.setBasePath("/api");
        config.exposeIdsFor(A.class);
        config.exposeIdsFor(B.class);
    }
}

当我尝试在表B中插入新行时,我在spring中遇到以下错误:

javax.validation.ConstraintViolationException: Validation failed for classes [......model.B] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=a, rootBeanClass=class ........model.B, messageTemplate='{javax.validation.constraints.NotNull.message}'}
]

我想知道http post请求的请求有效负载究竟是什么样子,以便应用程序将新的B行插入数据库。非常感谢任何帮助。

编辑:

我用这个映射做了一个弹簧休息控制器:

@PostMapping("/api/b")
@ResponseBody
public String createServis(@RequestBody B b){

}

如果我使用此有效负载发出http post请求(使用curl或其他方式,但这不是重点):

{
    id:1,
    aId:1
}

或者这个:

{
    id:1,
    a:{id:1}
}

甚至是这个:

{
    id:1,
    aId:1,
    a:{id:1}
}

对象b的属性a保持为null /未正确初始化。

1 个答案:

答案 0 :(得分:0)

我在这里得到了答案Spring Rest: Cannot insert rows that have @ManyToOne column

基本上你需要张贴:

{
    id: 1
    a: "http://localhost:4200/api/a/1"
}

到url localhost:4200 / api / b