Bladeren bron

modify:

1. 金额序列化;
tags/24082201
WendyYang 2 maanden geleden
bovenliggende
commit
b3b1660448
3 gewijzigde bestanden met toevoegingen van 52 en 40 verwijderingen
  1. +6
    -38
      hz-pm-api/src/main/java/com/hz/pm/api/common/config/ConfigurerAdapter.java
  2. +42
    -0
      hz-pm-api/src/main/java/com/hz/pm/api/common/config/ConfigurerAdapterPostConfig.java
  3. +4
    -2
      hz-pm-api/src/main/java/com/hz/pm/api/projectlib/helper/ProjectManageUtil.java

+ 6
- 38
hz-pm-api/src/main/java/com/hz/pm/api/common/config/ConfigurerAdapter.java Bestand weergeven

@@ -1,55 +1,23 @@
package com.hz.pm.api.common.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.hz.pm.api.common.interceptor.LogInterceptor;
import com.ningdatech.basic.config.BaseConfig;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
* @author qinxianyun
*/
@EnableWebMvc
@Configuration
public class ConfigurerAdapter implements WebMvcConfigurer {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
// 使用 fastjson 序列化,会导致 @JsonIgnore 失效,可以使用 @JSONField(serialize = false) 替换
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List<MediaType> supportMediaTypeList = new ArrayList<>();
supportMediaTypeList.add(MediaType.APPLICATION_JSON);
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
config.getSerializeConfig().addFilter(BigDecimal.class, (ValueFilter) (object, name, value) -> {
if (!(value instanceof BigDecimal)) {
return value;
}
// 保留两位小数
return ((BigDecimal) value).setScale(2, RoundingMode.HALF_UP).toPlainString();
});
converter.setFastJsonConfig(config);
converter.setSupportedMediaTypes(supportMediaTypeList);
converter.setDefaultCharset(StandardCharsets.UTF_8);
converters.add(converter);
}
@RequiredArgsConstructor
public class ConfigurerAdapter extends BaseConfig {

@Override
public void addInterceptors(@NonNull InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**").order(-100);
}


}

+ 42
- 0
hz-pm-api/src/main/java/com/hz/pm/api/common/config/ConfigurerAdapterPostConfig.java Bestand weergeven

@@ -0,0 +1,42 @@
package com.hz.pm.api.common.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
* <p>
* ConfigurerAdapterPostConfig
* </p>
*
* @author WendyYang
* @since 17:18 2024/8/21
*/
@Configuration
@AutoConfigureAfter(ConfigurerAdapter.class)
public class ConfigurerAdapterPostConfig {

public ConfigurerAdapterPostConfig(ObjectMapper objectMapper) {
SimpleModule module = new SimpleModule();
module.addSerializer(BigDecimal.class, new JsonSerializer<BigDecimal>() {
@Override
public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (value != null) {
gen.writeString(value.setScale(2, RoundingMode.HALF_UP).toPlainString());
} else {
gen.writeNull();
}
}
});
objectMapper.registerModule(module);
}

}

+ 4
- 2
hz-pm-api/src/main/java/com/hz/pm/api/projectlib/helper/ProjectManageUtil.java Bestand weergeven

@@ -1,5 +1,6 @@
package com.hz.pm.api.projectlib.helper;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.hz.pm.api.performance.model.entity.ProjectCoreBusinessIndicators;
@@ -86,8 +87,9 @@ public class ProjectManageUtil {
.notIn(CollUtil.isNotEmpty(req.getProjectCodes()), Project::getProjectCode, req.getProjectCodes())
.eq(req.getIsStartDeclaredProject() != null, Project::getAnnualPlanOpened, req.getIsStartDeclaredProject())
.orderByDesc(Project::getUpdateOn);
if (req.getBuildOrgCode() != null) {
List<Long> viewOrgCodes = getMhUnitCache().getViewChildIdsRecursion(req.getDeclaredUnitId());
// 处理申报单位查询
if (req.getBuildOrgCode() != null && NumberUtil.isLong(req.getBuildOrgCode())) {
List<Long> viewOrgCodes = getMhUnitCache().getViewChildIdsRecursion(Long.parseLong(req.getBuildOrgCode()));
query.in(Project::getBuildOrgCode, viewOrgCodes);
}
}


Laden…
Annuleren
Opslaan