Nell'ultimo periodo sto utilizzando Retrofit per creare client di accesso a api rest.
Un problema comune è quando si vuole passare nella richiesta in @Body in post un parametro con un modello di classe che contenga un campo java.util.Date. Il problema nasce dalla conversione in Json per il client che si aspetta tutti dati in json in ingresso e a sua volta restituisce un json.
Ho risolto il problema configurando il serializzatore e deserializzatore per il client okclient:
private static RestAdapter buildClientRestAdapter(String uri) {
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(ClientDispatcher.timeout, TimeUnit.MILLISECONDS);
okHttpClient.setConnectTimeout(ClientDispatcher.timeout, TimeUnit.MILLISECONDS);
final OkClient httpClient = new OkClient(okHttpClient);
final GsonBuilder builder = new GsonBuilder();
builder.setDateFormat(DateFormat.MILLISECOND_FIELD);
builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
return src == null ? null : new JsonPrimitive(src.getTime());
}
});
final Gson gson = builder.create();
return new RestAdapter.Builder().setEndpoint(uri).setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new GsonConverter(gson)).setClient(httpClient).build();
}
lunedì 17 novembre 2014
Client Retrofit - Problemi con java.util.Date
Ubicazione:
13048 Santhià VC, Italia
giovedì 13 novembre 2014
MongoDB - Script per profile su tutto il server
Abilita il profile per tutti i db del mongo server esclusi local e admin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dbs_list = db.getMongo().getDBNames(); | |
for(var d in dbs_list){ | |
var name = dbs_list[d]; | |
if(name == 'admin') | |
continue; | |
if(name == 'local') | |
continue; | |
dbOne = db.getSisterDB(name); | |
dbOne.setProfilingLevel(0); | |
dbOne.system.profile.drop(); | |
dbOne.createCollection("system.profile", {capped:true, size:8000000}); | |
dbOne.setProfilingLevel(2); | |
print(name + " --> complete"); | |
}; |
per interrogare tutti i profile con l'elenco delle ultime 5 query che superano i 5 millis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dbs_list = db.getMongo().getDBNames(); | |
for(var d in dbs_list){ | |
var name = dbs_list[d]; | |
if(name == 'admin' || name == 'local') | |
continue; | |
dbOne = db.getSisterDB(name); | |
print("dbname -->" + name); | |
var cursor = dbOne.system.profile.find( { op:"query", millis : { $gte : 5 },ns : { $ne : (name + '.system.profile') } },{ ts:1,millis:1, nscanned:1, nreturned:1, ns:1, query:1} ).sort({ts:-1}).limit(5); | |
cursor.forEach(function(r) { | |
print(JSON.stringify(r)); | |
}); | |
print(); | |
print(); | |
} |
Etichette:
javascript,
mongodb,
performance,
profile
Ubicazione:
13048 Santhià VC, Italia
Iscriviti a:
Post (Atom)