SDR:从关联对象的json中排除字段

时间:2016-02-01 18:14:54

标签: spring-data-rest

我有这样的实体:

RandomAccessFile r = new RandomAccessFile(String path, "r");

int header_read;
int header_remaining = 4; // header length, initially

byte[]     ba = new byte[header_remaining];
ByteBuffer bb = new ByteBuffer.allocate(header_remaining);

while ((header_read = r.read(ba, 0, header_remaining) > 0) {
    header_remaining -= header_read;
    bb.put(ba, 0, header_read);
}

byte[] header = bb.array();

// process header, not shown
// the RandomAccessFile above reads only a small amount, so buffering isn't required

r.seek(0);

FileInputStream f_1 = new FileInputStream(r.getFD());

Library1Result result1 = library1.Main.entry_point(f_1)

// process result1, not shown
// Library1 reads the InputStream in large chunks, so buffering isn't required
// invalidate f_1 (this question)

r.seek(0)

int read;
while ((read = r.read(byte[4096] buffer)) > 0 && library1.continue()) {
    library2.process(buffer, read);
}

// the RandomAccessFile above is read in large chunks, so buffering isn't required
// in a previous edit the RandomAccessFile was used to create a FileInputStream. Obviously that's not required, so ignore

r.seek(0)

Reader r_1 = new BufferedReader(new InputStreamReader(new FileInputStream(r.getFD())));

Library3Result result3 = library3.Main.entry_point(r_2)

// process result3, not shown
// I'm not sure how Library3 uses the reader, so I'm providing buffering
// invalidate r_1 (this question) - bonus: frees the buffer

r.seek(0);

FileInputStream f_2 = new FileInputStream(r.getFD());

Library1Result result1 = library1.Main.entry_point(f_2)

// process result1 (reassigned), not shown
// Yes, I actually have to call 'library1.Main.entry_point' *again* - same comments apply as from before
// invalidate f_2 (this question)
//
// I've been told to be careful when opening multiple streams from the same
// descriptor if one is buffered. This is very vague. I assume because I only
// ever use any stream once and exclusively, this code is safe.
//

当我做GET /餐馆时,我想内联经理,但只有名字,而不是lastLogin。

当我做GET / users时,我想看到完整的用户:name + lastLogin

所以我创建了一个餐厅投影并将其作为摘录应用于RestaurantRepo。但这会将整个用户嵌入餐厅。

当我为用户创建一个摘录项目时,它省略了lastLogin字段,然后它按预期用于餐馆,但不适用于用户,因为这里缺少lastLogin。

我该如何解决这个问题?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作(例如spring data rest documentation

@Projection(name = "virtual", types = { Person.class })
public interface VirtualProjection {

  @Value("#{target.firstName} #{target.lastName}") 
  String getFullName();

}

因此,在您的情况下,这将导致此预测:

@Projection(name = "restaurantWithManagerName", types = { Restaurant.class })
public interface RestaurantWithManagerNameProjection {

  String getName();

  @Value("#{target.manager.name}") 
  String getManagerName();

}

如果您想使用嵌套的User对象保留原始结构,可以通过为用户创建自定义缩小界面并在投影中使用它来实现此目的:

public interface UserLight {
    String getName();
}


@Projection(name = "restaurantWithReducedManager", types = Restaurant.class)
public interface RestaurantWithManagerNameProjection {

    String getName();
    UserLight getManager();
}