spring-data-rest集成测试因简单的json请求而失败 [英] spring-data-rest integration test fails with simple json request

查看:90
本文介绍了spring-data-rest集成测试因简单的json请求而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的spring-data-rest集成测试因简单的json请求而失败。考虑下面的jpa模型

My spring-data-rest integration test fails for a simple json request. Consider the below jpa models

Order.java

public class Order {

    @Id @GeneratedValue//
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY)//
    private Person creator;
    private String type;

    public Order(Person creator) {
        this.creator = creator;
    }

    // getters and setters
}

Person.java

ic class Person {

    @Id @GeneratedValue private Long id;

    @Description("A person's first name") //
    private String firstName;

    @Description("A person's last name") //
    private String lastName;

    @Description("A person's siblings") //
    @ManyToMany //
    private List<Person> siblings = new ArrayList<Person>();

    @ManyToOne //
    private Person father;

    @Description("Timestamp this person object was created") //
    private Date created;

    @JsonIgnore //
    private int age;

    private int height, weight;
    private Gender gender;

    // ... getters and setters
}

在我的测试中,我通过使用personRepository创建了一个人,并通过传递人员来创建了一个人

In my test I created a person by using personRepository and inited order by passing person

Person creator = new Person();
creator.setFirstName("Joe");
creator.setLastName("Keith");
created.setCreated(new Date());
created.setAge("30");
creator = personRepository.save(creator);

Order order = new Order(creator);
String orderJson = new ObjectMapper().writeValueAsString(order);

mockMvc.perform(post("/orders").content(orderJson).andDoPrint());

订单已创建,但创建者未与订单关联。另外我想将请求体传递为json对象。在这个我的json对象应该包含如下创建者

Order is created but creator is not associated with the order. Also I want to pass request body as a json object. In this my json object should contain creator as follows

{
"type": "1",
"creator": {
    "id": 1,
    "firstName": "Joe",
    "lastName": "Keith",
    "age": 30
}
}

如果我发送带有以下json的请求正文,则通话工作正常

If I send request body with the following json, the call works fine

{
"type": "1",
"creator": "http://localhost/people/1"
}

但我不想发送第二个json。不知道如何解决这个问题。因为我的客户端已经通过发送第一个json来消耗服务器响应。现在我迁移了我的服务器以使用spring-data-rest。之后我的所有客户端代码都无法正常工作。

But I don't want to send the second json. Any idea how to solve the issue. Because already my client is consuming the server response by sending first json. Now I migrated my server to use spring-data-rest. After that all my client code is not working.

如何解决这个问题?

推荐答案

您是否尝试在@ManyToOne注释中使用 cascade = CascadeType.ALL

Did you try using cascade = CascadeType.ALL in @ManyToOne annotation


public class Order {
    @Id @GeneratedValue//
    private Long id;
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)//
    private Person creator;
    private String type;

    public Order(Person creator) {
        this.creator = creator;
    }

    // getters and setters
}


这篇关于spring-data-rest集成测试因简单的json请求而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆