alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.75k stars 6.5k forks source link

Spring boot Feign 泛型返回 Result<T> json反序列化 丢失 #1914

Open tosslife opened 6 years ago

tosslife commented 6 years ago

1.2.47版本 jdk1.8 feign请求结果反序列化时候 json格式是中 personid 转换成 model 驼峰 personId 则结果丢失 但是 json中 name 对应model中 name 这种没有 对应非驼峰的 不会丢失

    SerializeConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
    ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;

已设置 但仍旧是无效

ljluestc commented 5 months ago

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.ParserConfig;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.PropertyNamingStrategy;

public class Main {
    public static void main(String[] args) {
        // Set property naming strategy for serialization
        SerializeConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;

        // Set property naming strategy for deserialization
        ParserConfig.getGlobalInstance().propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;

        // Your JSON data
        String jsonData = "{\"person_id\": 123, \"name\": \"John Doe\"}";

        // Deserialize JSON into your model class
        Person person = JSON.parseObject(jsonData, Person.class);

        // Print the deserialized object
        System.out.println(person.getPersonId()); // This should print the value of person_id
        System.out.println(person.getName());     // This should print the value of name
    }

    // Your model class
    static class Person {
        private int personId;
        private String name;

        // Getters and setters
        public int getPersonId() {
            return personId;
        }

        public void setPersonId(int personId) {
            this.personId = personId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}