我的应用程序是使用Spring boot(1.3.3.RELEASE)和spring mvc,spring data jpa hibernate构建的.MySql是数据库,Jackson是Json序列化器.在java 8上.
我想在我的控制器方法中返回一个庞大的数据集.我想要返回如下所示的对象流,而不是检索所有数据然后传入jackson序列化器.
@RequestMapping(value = "/candidates/all", method = RequestMethod.GET)
public Stream<Candidate> getAllCandidates(){
try {
return candidateDao.findAllByCustomQueryAndStream();
} catch(Exception e){
LOG.error("Exception in getCandidates",e);
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
我的DAO如下:
@Query("select c from Candidate c")
public Stream<Candidate> findAllByCustomQueryAndStream();
Run Code Online (Sandbox Code Playgroud)
但是,Jackson正在序列化流对象而不是流的内容.实际输出如下:
{"parallel" : false}
Run Code Online (Sandbox Code Playgroud)
如何指示Jackson序列化内容而不是流对象?
任何人都可以解释为什么下面的代码不能编译但第二个代码呢?
不要编译
private void doNotCompile() {
List<Integer> out;
out = IntStream
.range(1, 10)
.filter(e -> e % 2 == 0)
.map(e -> Integer.valueOf(2 * e))
.collect(Collectors.toList());
System.out.println(out);
}
Run Code Online (Sandbox Code Playgroud)
收集行上的编译错误
编译
private void compiles() {
List<Integer> in;
in = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
List<Integer> out;
out = in.stream()
.filter(e -> e % 2 == 0)
.map(e -> 2 * e)
.collect(Collectors.toList());
System.out.println(out);
}
Run Code Online (Sandbox Code Playgroud)