简体   繁体   English

Spring Boot RestController JSON序列化花费太多时间

[英]spring boot RestController JSON serialization cost too much time

I am developing a restful service using latest version of spring boot. 我正在使用最新版本的Spring Boot开发宁静的服务。 Here is a RestController: 这是一个RestController:

@GetMapping(path = "/table")
    public Iterable<Obsidian> getReactTable(@RequestParam Long orderId) {
            long start = System.currentTimeMillis();
            ArrayList<Obsidian> results = new ArrayList<Obsidian>();
            for (Obsidian obs : obsidianRepo.findByOrder(order)) {
                results.add(obs);
            }
            long end = System.currentTimeMillis();
//  Only cost about 300ms
//          System.out.println(end - start);
            return results;
        }
    }

the results list has about 500 Obsidians instances in total. 结果列表总共包含约500个Obsidians实例。

the Hibernate only cost me 300ms , and the JSON serialization (plus nio and web transfer) cost me 30 seconds! Hibernate只花了我300毫秒,而JSON序列化(加上nio和网络传输)花了我30秒钟!

What may cause Jackson be so slow? 是什么导致杰克逊这么慢?

btw: How I found it 30 seconds: I measure it in browser ajax. 顺便说一句:我怎么发现它30秒:我在浏览器ajax中对其进行测量。

Short answer: Jackson is fast. 简短的回答:杰克逊很快。

Long answer: 长答案:

I have to say sorry, I made a big mistake. 我不得不说对不起,我犯了一个大错误。 The POJO serialization is not that simple as I thought, it extends some base entity class, and the base class serialization is doing something slow in the database query. POJO序列化并不像我想的那么简单,它扩展了一些基本实体类,并且该基类序列化在数据库查询中做得很慢。

If you come across the question, check your POJO carefully. 如果您遇到问题,请仔细检查您的POJO。

set show-sql=true will help you find out the problem. 设置show-sql = true将帮助您找出问题所在。

Old mistake: 旧错误:

in order to prove that jackson is really slow, I add some code here: 为了证明杰克逊真的很慢,我在这里添加一些代码:

        long start = System.currentTimeMillis();
        ObjectMapper mapper  = new ObjectMapper();
        ArrayList<Obsidian> results = new ArrayList<Obsidian>();
        for (Obsidian obs : obsidianRepo.findByOrder(order)) {
            results.add(obs);
            mapper.writeValueAsString(obs);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

as you can see, I manually call jackson com.fasterxml.jackson.databind.ObjectMapper in the for loop to see how much time it cost, and the result is: 33247ms, which is exactly the time I measured in browser ajax. 如您所见,我在for循环中手动调用jackson com.fasterxml.jackson.databind.ObjectMapper以查看它花费了多少时间,其结果是:33247ms,这正是我在浏览器ajax中测量的时间。

@Test 
public void testJackson() throws JsonProcessingException {

    Obsidian obs = new Obsidian();
    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValueAsString(obs);
}

I also add a Test in JUnit Test, it cost 0.070s to complete. 我还在JUnit Test中添加了一个Test,它花费了0.070s来完成。

as a Gson compare: 作为Gson比较:

@Test 
public void testJackson() throws JsonProcessingException {
    Obsidian obs = new Obsidian();
    Gson gson = new Gson();
    gson.toJson(obs);
}

Gson only cost 0.018s. Gson仅花费0.018s。 (still too slow) (仍然太慢)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM