Gson: How to exclude specific fields from Serialization without annotations

后端 未结 15 1748
后悔当初
后悔当初 2020-11-22 05:39

I\'m trying to learn Gson and I\'m struggling with field exclusion. Here are my classes

public class Student {    
  private Long                id;
  privat         


        
15条回答
  •  我在风中等你
    2020-11-22 06:19

    I'm working just by putting the @Expose annotation, here my version that I use

    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    

    In Model class:

    @Expose
    int number;
    
    public class AdapterRestApi {
    

    In the Adapter class:

    public EndPointsApi connectRestApi() {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(90000, TimeUnit.SECONDS)
                .readTimeout(90000,TimeUnit.SECONDS).build();
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ConstantRestApi.ROOT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    
        return retrofit.create  (EndPointsApi.class);
    }
    

提交回复
热议问题