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

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

问题描述

对于一个简单的 json 请求,我的 spring-data-rest 集成测试失败.考虑以下 jpa 模型

Order.java

公共类订单{@Id @GeneratedValue//私人长ID;@ManyToOne(fetch = FetchType.LAZY)//个人创建者;私有字符串类型;公共秩序(个人创建者){this.creator = 创造者;}//getter 和 setter}

Person.java

ic class Person {@Id @GeneratedValue 私有长 ID;@Description("一个人的名字")//私人字符串名字;@Description("一个人的姓氏")//私人字符串姓氏;@Description("一个人的兄弟姐妹")//@ManyToMany//私人名单<人>兄弟姐妹 = 新的 ArrayList();@ManyToOne//私人父亲;@Description("创建此人对象的时间戳")//私人日期创建;@JsonIgnore//私人整数年龄;私人 int 身高、体重;私人 性别 性别;//... getter 和 setter}

在我的测试中,我使用 personRepository 创建了一个人,并通过传递 person 来初始化顺序

Person creator = new Person();creator.setFirstName("乔");creator.setLastName("基思");created.setCreated(new Date());created.setAge("30");创造者 = personRepository.save(creator);订单订单=新订单(创建者);String orderJson = new ObjectMapper().writeValueAsString(order);mockMvc.perform(post("/orders").content(orderJson).andDoPrint());

订单已创建,但创建者与订单无关.我也想将请求正文作为 json 对象传递.在这个我的 json 对象应该包含创建者如下

<代码>{"类型": "1",创造者":{身份证":1,"firstName": "乔","lastName": "基思",年龄":30}}

如果我使用以下 json 发送请求正文,则调用正常

<代码>{"类型": "1","creator": "http://localhost/people/1"}

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

如何解决这个问题?

解决方案

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

<块引用>

公共类订单{@Id @GeneratedValue//私人长ID;@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)//个人创建者;私有字符串类型;公共秩序(个人创建者){this.creator = 创造者;}//getter 和 setter}

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
}

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());

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
}
}

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

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

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.

How to solve this?

解决方案

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天全站免登陆