Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.ibeetl.admin.core.conf;
 
import java.io.IOException;
import java.text.SimpleDateFormat;
 
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.ibeetl.admin.core.web.JsonResult;
 
@Configuration
public class JasonConfig {
    @Bean
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
        SimpleModule simpleModule = new SimpleModule("SimpleModule", 
                Version.unknownVersion());
        simpleModule.addSerializer(JsonResult.class, new CustomJsonResultSerializer());
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
    /**
     * layui 前端要求后台返回的数据格式
     * @author xiandafu
     *
     */
    public static class CustomJsonResultSerializer extends JsonSerializer<JsonResult> {
 
        public CustomJsonResultSerializer() {
        }
 
 
        @Override
        public void serialize(JsonResult value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            gen.writeStartObject();
            if(value.getCode().equals("200")) {
                gen.writeObjectField("code", 0);
            }else {
                gen.writeObjectField("code", Integer.parseInt(value.getCode()));
            }
            gen.writeStringField("msg", value.getMsg());
            Object data = value.getData();
            if(data instanceof PageQuery ) {
                PageQuery query = (PageQuery)(data);
                gen.writeObjectField("count", query.getTotalRow());
                gen.writeObjectField("data", query.getList());
            }else {
                
                gen.writeObjectField("data", data);
            }
            gen.writeEndObject();
        }
 
    }
}