![JAR search and dependency download from the Maven repository](/logo.png)
se.alipsa.simplerest.Response Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of simple-rest Show documentation
Show all versions of simple-rest Show documentation
Simple, modular Rest library for java 11+
The newest version!
package se.alipsa.simplerest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
import java.util.Map;
/**
* The Response class is a core part of the simple-rest api.
* It contains the "raw" json string returned from the server and also the response code and header fields.
* The response can
*/
public class Response {
private String payload;
private int responseCode;
private Map> headers;
private final ObjectMapper objectMapper;
/**
* Default constructor, will create a vanilla ObjectMapper for use in subsequent calls.
*/
public Response() {
objectMapper = new ObjectMapper();
}
/**
*
* @param payload the "raw" content of the json response
* @param responseCode the HTTP status code, e.g. 200 for OK etc.
* @param headers a map of the HTTP Header content
* @param prefObjectMapper an optional parameter for using a preferred Object Mapper other than the default on
*/
public Response(String payload, int responseCode, Map> headers, ObjectMapper... prefObjectMapper) {
this.payload = payload;
this.responseCode = responseCode;
this.headers = headers;
objectMapper = prefObjectMapper.length > 0 ? prefObjectMapper[0] : new ObjectMapper();
}
/**
* Converts the json payload into a Java object
* @param returnClass the type of Java Object to return
* @param the type of Java Object to return
* @param customMapper an optional ObjectMapper
* @return the java Object corresponding to the payload and the returnClass
* @throws JsonProcessingException if the conversion failed.
*/
public T getObject(Class returnClass, ObjectMapper... customMapper) throws JsonProcessingException {
ObjectMapper mapper = customMapper.length > 0 ? customMapper[0] : objectMapper;
return mapper.readValue(getPayload(), returnClass);
}
/**
* Converts the json payload into a List of Java objects
* @param returnClass the type of Java Object to return
* @param the type of Java Object in the List to return
* @param customMapper an optional ObjectMapper
* @return a List of the type corresponding to the returnClass provided
* @throws JsonProcessingException if the conversion failed.
*/
public List getObjectList(Class returnClass, ObjectMapper... customMapper) throws JsonProcessingException {
ObjectMapper mapper = customMapper.length > 0 ? customMapper[0] : objectMapper;
// does not work, content is returned as List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy