自定义注解结合继承JsonSerialize实现ContextualSerializer,实现返回结果转译

注解结合继承JsonSerialize实现ContextualSerializer,通过注解显式的声明序列化方式,实现返回的对象进行转译。

代码示例:

1、自定义注解

/**
 * @Author hmh
 * @Description 嵌入式转译注解,生效点:request请求完成返回时,进行序列化过滤,并嵌入实现
 * @Description 失效点: service业务处理层是不会生效的,序列化对象转Json,或者其他格式对象后,也是无法过滤到的
 **/
@JacksonAnnotationsInside
@JsonSerialize(using = DicTransContextualSerializer.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EmbedTrans {
    /**
     * dic类型
     *
     * @return
     */
    @NotBlank
    String dictType();

    /**
     * 转译输出字段
     *
     * @return
     */
    String fieldName() default "";

    /**
     * 需要调用查询字典的方法
     *
     * @return
     */
    DicTypeEnum dicMetohd() default DicTypeEnum.NORMAL_CODE_TRANS_NAME;
}

 2,实现ContextualSerializer获取回调

public class DicTransContextualSerializer extends JsonSerializer<String> implements ContextualSerializer {
    private String dictType;
    private String fieldName;
    private DicTypeEnum dicTypeEnum;

    public DicTransIntegerSerializer(String dictType, String fieldName, DicTypeEnum dicTypeEnum) {
        this.dictType = dictType;
        this.fieldName = fieldName;
        this.dicTypeEnum = dicTypeEnum;
    }

    public DicTransIntegerSerializer() {
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        if (beanProperty != null) {
            if (Objects.equals(beanProperty.getType().getRawClass(), int.class)
                    || Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
                EmbedTrans t = beanProperty.getAnnotation(EmbedTrans.class);
                if (t != null) {
                    String beanFieldName = beanProperty.getName();
                    if (StringUtils.hasText(t.fieldName())) {
                        beanFieldName = t.fieldName();
                    }
                    DicTypeEnum dicTypeEnum = DicTypeEnum.NORMAL_CODE_TRANS_NAME;
                    if (t.dicMetohd() != null) {
                        dicTypeEnum = t.dicMetohd();
                    }
                    if (Objects.equals(beanProperty.getType().getRawClass(), int.class)) {
                        return new DicTransIntegerSerializer(t.dictType(), beanFieldName + "Text", dicTypeEnum);
                    } else {
                        return new DicTransStringSerializer(t.dictType(), beanFieldName + "Text", dicTypeEnum);
                    }
                }
            }
            return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
        }
        return serializerProvider.findNullValueSerializer(beanProperty);
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeObject(integer);
        jsonGenerator.writeFieldName(fieldName);

        String val = String.valueOf(integer);
        String getval = DicUtil.getval(dicTypeEnum, dictType, val);

        jsonGenerator.writeString(getval);
    }

博主原文:自定义注解结合继承JsonSerialize实现ContextualSerializer,实现返回结果转译-CSDN博客

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是使用C语言实现将数字转换成字母的代码示例: ```c #include <stdio.h> int main() { int num = 65; // 将数字65转换成字母'A' char letter = (char)num; // 强制类型转换 printf("The letter is %c\n", letter); // 输出字母'A' return 0; } ``` 在这个示例中,我们将数字65强制转换为char类型,然后将其赋值给变量letter。最后,我们使用printf函数将字母'A'输出到控制台。 请注意,这只适用于将数字转换为其ASCII码等价物的情况。如果要将数字转换为其他字母系统中的字母,需要使用不同的转换方法。 ### 回答2: 在C语言中,可以使用字符数组和字符串的特性来实现将数字转译成字符的功能。 首先,可以定义一个字符数组来存储对应的字符: ```c char digits[10] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; ``` 这个数组中,每个元素表示对应的数字字符。 接下来,可以编写一个函数来实现将数字转译成字符的操作: ```c void convertToChar(int number, char *result) { int count = 0; int temp = number; // 计算数字的位数 while (temp != 0) { temp /= 10; count++; } // 将数字从低位到高位转译成字符 for (int i = count-1; i >= 0; i--) { // 从最高位开始 result[i] = digits[number % 10]; // 取余数作为索引,从digits数组中取对应的字符 number /= 10; // 去掉最低位 } result[count] = '\0'; // 字符串末尾添加结束符 } ``` 这个函数接受一个整数和一个字符指针作为参数,将对应的字符存储在字符指针指向的数组中。 可以在主函数中调用这个函数进行测试: ```c int main() { int num = 12345; char charArray[6]; convertToChar(num, charArray); printf("%s\n", charArray); return 0; } ``` 运行程序后,输出将会是: ``` 12345 ``` 这样就实现了将数字转译成字符的功能。 ### 回答3: 在C语言中,我们可以使用类型转换来将数字转译成字符。 首先,我们需要了解ASCII码。ASCII码是一种常用的字符编码标准,用于将字符映射到数字。在ASCII码表中,每个字符都对应着一个唯一的数字。 为了将数字转译成字符,我们可以使用类型转换操作符`(char)`。这个操作符可以将一个整数类型的值转换为字符类型。 下面是一个简单的示例代码,演示了如何将一个数字转译成字符: ```c #include <stdio.h> int main() { int number = 65; // 假设我们要将数字65转译成字符,对应ASCII码中的字母'A' char character = (char)number; // 使用类型转换将数字转换为字符 printf("转译后的字符为: %c\n", character); return 0; } ``` 运行上述代码,我们将会得到输出结果为: ``` 转译后的字符为: A ``` 在这个示例中,我们将数字65转译成了字符'A'。使用`(char)`操作符将整数类型的数字转换成字符类型,并通过`printf`函数将转译后的字符输出到控制台。 需要注意的是,当数字超出ASCII码表所能表示的范围时,转译结果可能会是不可打印的字符或者乱码。因此,在实际应用中,我们需要确保将数字转译成字符的操作是在ASCII码表所能包含的范围内。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值