Spring序列化

本文主要探讨Spring框架中的序列化过程,涉及使用场景,包括Controller参数处理和Enum的序列化,同时讲解了Deserializer在序列化过程中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

####Spring序列化

场景

Controller

package com.baidu.test
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

public class AuditController{
    @GetMapping("/detail.json")
    public Object countdetail(Long id){
        
    }
    
    @GetMapping("/countyList.json")
    public Object countList(CountryParam param){
        
    }
    
    @PostMapping("/addCountry.json")
    public Object addCountry(@RequestBody CountryParam param){
        
    }
    
}


Param

package com.baidu.test.param


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CountryParam{
	private CountryEnum country;
	private String name;
	private Integer peopleCount;
	private Long id;
}

Enum

package com.baidu.test.type;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;
import org.jooq.types.UInteger;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wangjinliang.wjl
 * @date 2019/01/14
 **/
public enum CountryEnum {// 在这里定义枚举值
    CHINA(1, "中国"),
    USA(2, "美国"),
    UK(3, "英国");

    @Getter
    @JsonValue
    private UInteger value;

    @Getter
    private String name;

    CountryEnum(Integer value, String name) {
        this.value = UInteger.valueOf(value);
        this.name = name;
    }

    @JsonCreator
    public static CountryEnum fromValue(Integer value) {
        return MAP.get(UInteger.valueOf(value));
    }

    public static CountryEnum fromValue(UInteger value) {
        return MAP.get(value);
    }


    public static CountryEnum fromValue(String value) {
        return MAP.get(UInteger.valueOf(value));
    }

    private static final Map<UInteger, CountryEnum> MAP = new HashMap<>();

    static {
        for (CountryEnum item : CountryEnum.class.getEnumConstants()) {
            MAP.put(item.getValue(), item);
        }
    }
}

Deserializer

package com.baidu.test.spring.deserializer;

import com.alibaba.soc3.type.CountryEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * @author wangjinliang.wjl
 * @date 2019/01/10
 **/
@Component
public class SpringConverterCountryEnumInteger implements Converter<String, CountryEnum> {

    @Override
    public CountryEnum convert(String stat) {
        if(StringUtils.isNumeric(stat)){
            Integer statCode = Integer.valueOf(stat);
            return CountryEnum.fromValue(statCode);
        }
        return CountryEnum.fromValue(stat);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值