de.qytera.qtaf.http.HTTPJsonDao Maven / Gradle / Ivy
The newest version!
package de.qytera.qtaf.http;
import de.qytera.qtaf.core.gson.GsonHelper;
import jakarta.ws.rs.core.MediaType;
/**
* A DAO for HTTP requests with JSON content.
*/
public class HTTPJsonDao extends HTTPDao {
/**
* Constructor.
*
* @param host host name
*/
public HTTPJsonDao(String host) {
super(host);
}
/**
* Send GET Request.
*
* @param path URL path
* @param tClass Entity class
* @param Entity Type
* @return Entity object
*/
public T get(String path, Class tClass) {
String json = super.getAsString(path, MediaType.APPLICATION_JSON);
return GsonHelper.fromJson(json, tClass);
}
/**
* Send POST Request.
*
* @param path URL path
* @param tClass Entity class
* @param Entity Type
* @param requestEntity Request payload
* @return Entity object
*/
public T post(String path, Class tClass, Object requestEntity) {
String json = super.postAsString(path, MediaType.APPLICATION_JSON, gson.toJson(requestEntity));
return GsonHelper.fromJson(json, tClass);
}
/**
* Send PUT Request.
*
* @param path URL path
* @param tClass Entity class
* @param Entity Type
* @param requestEntity Request payload
* @return Entity object
*/
public T put(String path, Class tClass, Object requestEntity) {
String json = super.putAsString(path, MediaType.APPLICATION_JSON, gson.toJson(requestEntity));
return GsonHelper.fromJson(json, tClass);
}
/**
* Send DELETE Request.
*
* @param path URL path
* @param tClass Entity class
* @param Entity Type
* @return Entity object
*/
public T delete(String path, Class tClass) {
String json = super.deleteAsString(path, MediaType.APPLICATION_JSON);
return GsonHelper.fromJson(json, tClass);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy