jfinal接口開發的一些要點

service端: java

BaseResponse對象描述返回格式,如: sql

{
  "code": 1,
  "message": "成功查詢到該用戶信息",
  "data": {
    "name": "wuliao",
    "lover": "rose",
    "sex": 1,
    "email": "wuliao@gmail.com"
  }
}

 

DatumResponse與DataResponse繼承BaseResponse對象分別用來包裝單個對象與對象list; json

修改Model類增長新方法: app

public DatumResponse getById(String id){
     DatumResponse response = new DatumResponse();
     M modelClass = null;
        if (StringUtil.isNotEmptyOrNull(id)) 
         modelClass = findById(id);

        if (modelClass == null) {
            response.setCode(Code.FAIL).setMessage(getClass().getName()+" is not found");
        } else {
            response.setDatum(modelClass);
        }
     return response;
    }
    
    public DataResponse getList(String sql, Object... paras){
     DataResponse resp = new DataResponse();
  List<M> result = null;
        if (paras!=null) 
         result = find(sql,paras);                    
        if (result == null || result.size() == 0) {
            resp.setMessage("未查詢到數據");
        } else {
            resp.setMessage("success");
            resp.setData(result);
        }
  return resp;
    }
    
    public DatumResponse getFirst(String sql, Object... paras){
     DatumResponse response = new DatumResponse();
     M modelClass = null;
        if (paras!=null) 
         modelClass = findFirst(sql, paras);

        if (modelClass == null) {
            response.setCode(Code.FAIL).setMessage(getClass().getName()+" is not found");
        } else {
            response.setDatum(modelClass);
        }
     return response;
    }

 

    在controller中返回封裝對象便可,如: 設計

/**
     * 根據id號查詢單個用戶
     */
 public void getEmp() {
     renderJson(Emp.dao.getUserById(getPara("userId")));
    }
    /**
     * 查詢某機構用戶列表
     */
    public void getUserListOfOrg() {
     renderJson(Emp.dao.getUserListOfOrg(getPara("orgId")));
    }


 

portal端: code

主要是jackson的解析須要注意ObjectMapper的一些設置,如: orm

ObjectMapper objectMapper = Jackson.getJson().getObjectMapper();
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        objectMapper.setSerializationInclusion(Include.NON_NULL);
        objectMapper.getDeserializationConfig().withoutFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));                    

        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        objectMapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

 

另外一點須要注意的是單對象的轉換與list的對象轉稍有差別: xml

單對象: 對象

return objectMapper.readValue(jsonString, type);

 

對象list: 繼承

TypeFactory t = TypeFactory.defaultInstance();

return objectMapper.readValue(jsonString,t.constructCollectionType(ArrayList.class,type));

 

 

 備註:參考了空谷幽蘭的設計,TKS

相關文章
相關標籤/搜索