GWT Autobean set initial value of created interface

不问归期 提交于 2019-12-11 18:26:53

问题


We are using AutoBeans to create our Pojo objects for use in RPC-Calls. What is the recommended approach for the Pojo to have a default value or other class initialization?

For example

 public interface SamplePojo {
        // should default to 5
        int getSampleProperty();
        void setSampleProperty(int sampleProperty);
    }


    public interface ModelFactory extends AutoBeanFactory {
        AutoBean<SamplePojo> getSamplePojo();   
    }

And SamplePojo has a int property that we always want to default to 5.


回答1:


AutoBeans should be seen as low-level, mapping straight to/from JSON. With that in mind, you don't want getSampleProperty() to be 5, you rather want to detect the absence of specific value for the property and use 5 in that case.

So, if 0 (the default value of an int) is not an acceptable value for the property, then simply "use 5 if the property is 0". Otherwise, change the return type to Integer and "use 5 if the property is null".




回答2:


Would this work?

public interface SamplePojo {
        // should default to 5
        int getSampleProperty();
        void setSampleProperty(int sampleProperty);
    }

public class SamplePojoImpl implements SamplePojo{
    private int sampleProperty = 5
    // getters/setters
    int getSampleProperty(){ return sampleProperty;}
    void setSampleProperty(int sampleProperty){this.sampleProperty = sampleProperty;}

}

public interface ModelFactory extends AutoBeanFactory {
    AutoBean<SamplePojo> getSamplePojo(SamplePojoImpl samplePojo );   
}


来源:https://stackoverflow.com/questions/13011520/gwt-autobean-set-initial-value-of-created-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!