Announcement Announcement Module
Collapse
No announcement yet.
How to set a property in Abstract class when using RestTemplate? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set a property in Abstract class when using RestTemplate?

    I want to set the "mId" property in abstract class when I using RestTemplate,and restful service sending a "user_id" or "post_id".
    No matter extended class using userId or not, the only thing I want is making "mId" be right.
    1. I defined an abstract class to access sqlite:
    Code:
    public abstract class Model {
    	protected Long mId = null;
    	public final Long getId() {
    		return mId;
    	}
    	
    	public void setId(Long id){
    		mId = id;
    	}
    
    	public void saveToSqlite(){
    		//using mId;
    	}
    }
    2. An class for restful user data:
    Code:
    public class User extends Model implements Serializable{
    	private static final long serialVersionUID = 1L;
    	@SerializedName("user_id")
    	private Long userId;
    	
    	public Long getUserId() {
    		return userId;
    	}
    
    	public void setUserId(Long userId) {
    		this.userId = userId;
    		this.setId(userId);
    	}
    }
    3. my request code:
    Code:
    	RestTemplate restTemplate = new RestTemplate();
    	GsonHttpMessageConverter jsonConverter = new GsonHttpMessageConverter();
    	restTemplate.getMessageConverters().add(jsonConverter);
    	final String url = "http://restful.test.com/users";
    	User[] users = restTemplate.getForObject(url, User[].class);
    the setUserId function is not accessed ,so mId is not set. How I can deal with it?
    Thx!
    Last edited by allending; Apr 11th, 2013, 01:48 AM.

  • #2
    I've got an answer on stackoverflow.

    Hope this also helps other users here.

    http://stackoverflow.com/questions/1...ith-spring-for

    Comment

    Working...
    X