Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring在序列化期间获取延迟加载的对象_Java_Spring - Fatal编程技术网

Java Spring在序列化期间获取延迟加载的对象

Java Spring在序列化期间获取延迟加载的对象,java,spring,Java,Spring,我拥有以下实体: @Data public class Project { // attributes @OneToMany private List<Employee> employees; } 这标志着序列化的开始 如果我自己事先没有明确地获取延迟加载的对象,我怎么能告诉spring在序列化过程中忽略这些对象呢 编辑: Spring在类MethodSecurityInterceptor中获取此方法内的对象: public Object invoke(Method

我拥有以下实体:

@Data
public class Project {

  // attributes

  @OneToMany
  private List<Employee> employees;
}
这标志着序列化的开始

如果我自己事先没有明确地获取延迟加载的对象,我怎么能告诉spring在序列化过程中忽略这些对象呢

编辑:

Spring在类
MethodSecurityInterceptor
中获取此方法内的对象:

public Object invoke(MethodInvocation mi) throws Throwable {
    InterceptorStatusToken token = super.beforeInvocation(mi);

    Object result;
    try {
      result = mi.proceed(); // this line causes it
    } finally {
      super.finallyInvocation(token);
    }

    return super.afterInvocation(token, result);
  }

这是在我的代码执行后调用的,我不确定为什么以及如何阻止调用。

如果您只发送数据,请使用。 为项目添加视图:

公共界面项目视图{
///例如,要公开的getter
字符串getName();
}
在projectRepository中添加:

List findAll();

如果
列表
作为json发送,那么您可以在“员工”上添加
@JsonIgnore
。或者使用DTO发送您需要的内容:我尝试了,但是用于获取
员工的SQL语句仍然是这样执行的。我想这会起作用,但是如果不使用视图/DTO,就无法实现吗?我想我无法在实体中公开
员工的getter
。如果需要,我可以用SQL语句返回它们,然后在单独的请求中返回
角色。这似乎比创建视图/dto的工作量要少,尤其是如果我需要计算一些东西,而如果我使用dto,这些东西将导致大量转换。@M.Dietz您可以尝试在
getProjects
上使用
@Transactional(readOnly=true)
。在那之后,你可以使用
@JsonIgnore
好的,我终于得到了它,我不得不用lomboks
@ToString(exclude={“employees”})
注释类,用
@JsonIgnore
注释字段。因此,在调用objects-toString方法并将其转换为JSON时,该字段似乎是延迟获取的。我知道第二个案例是个问题,但不知道调用了
toString
,即使我已经使用了@JsonIgnore。是因为它调用toString以在springs调试消息中显示对象吗?不确定它会在哪里调用toString。
List<Project> getProjects() {
  return projectRepository.findAll();
}
// gets the projects here
2019-12-23 13:46:03.759 DEBUG 9436 --- [nio-8080-exec-5] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Using 'application/json', given [application/json, text/plain, */*] and supported [application/json, application/*+json, application/json, application/*+json]
// gets the employees here
public Object invoke(MethodInvocation mi) throws Throwable {
    InterceptorStatusToken token = super.beforeInvocation(mi);

    Object result;
    try {
      result = mi.proceed(); // this line causes it
    } finally {
      super.finallyInvocation(token);
    }

    return super.afterInvocation(token, result);
  }