Record Class RestServiceResponse
java.lang.Object
java.lang.Record
com.bonitasoft.processbuilder.records.RestServiceResponse
- Record Components:
statusCode- The HTTP status codeheaders- Response headersbody- The response body as a stringcontentType- The content type of the responseexecutionTimeMs- Time taken to execute the request in millisecondserrorMessage- Error message if the request failed (null if successful)url- The URL that was called
public record RestServiceResponse(int statusCode, Map<String,String> headers, String body, RestContentType contentType, long executionTimeMs, String errorMessage, String url)
extends Record
Represents a REST service response.
This record contains all the information from a REST API response, including the status code, headers, body, and execution metadata.
- Since:
- 1.0
- Author:
- Bonitasoft
-
Constructor Summary
ConstructorsConstructorDescriptionRestServiceResponse(int statusCode, Map<String, String> headers, String body, RestContentType contentType, long executionTimeMs, String errorMessage, String url) Compact constructor with defaults. -
Method Summary
Modifier and TypeMethodDescriptionbody()Returns the value of thebodyrecord component.<T> Optional<T>Parses the response body as a specific type.Optional<com.fasterxml.jackson.databind.JsonNode>Parses the response body as JSON.Returns the value of thecontentTyperecord component.final booleanIndicates whether some other object is "equal to" this one.static RestServiceResponseCreates an error response.Returns the value of theerrorMessagerecord component.longReturns the value of theexecutionTimeMsrecord component.static RestServiceResponsefromException(Exception exception, long executionTimeMs, String url) Creates an error response from an exception.Gets a specific header value (case-insensitive).getJsonField(String fieldName) Gets a specific field from the JSON body.Gets the Location header (for redirects).final inthashCode()Returns a hash code value for this object.booleanChecks if the response body contains JSON content.headers()Returns the value of theheadersrecord component.booleanChecks if the response indicates a client error (4xx status code).booleanisError()Checks if there was an error (network error or non-success HTTP status).booleanChecks if the response was a redirect (3xx status code).booleanChecks if the response indicates a server error (5xx status code).booleanChecks if the response indicates success (2xx status code).intReturns the value of thestatusCoderecord component.static RestServiceResponsesuccess(int statusCode, Map<String, String> headers, String body, RestContentType contentType, long executionTimeMs, String url) Creates a successful response.final StringtoString()Returns a string representation of this record class.Returns a summary of the response for logging.url()Returns the value of theurlrecord component.Creates a copy of this response with a different body.Creates a copy of this response with a different error message.
-
Constructor Details
-
Method Details
-
success
public static RestServiceResponse success(int statusCode, Map<String, String> headers, String body, RestContentType contentType, long executionTimeMs, String url) Creates a successful response.- Parameters:
statusCode- The HTTP status codeheaders- Response headersbody- Response bodycontentType- Content type of the responseexecutionTimeMs- Execution timeurl- The URL called- Returns:
- A successful response
-
error
Creates an error response.- Parameters:
errorMessage- The error messageexecutionTimeMs- Execution timeurl- The URL that was called- Returns:
- An error response with status code -1
-
fromException
public static RestServiceResponse fromException(Exception exception, long executionTimeMs, String url) Creates an error response from an exception.- Parameters:
exception- The exception that occurredexecutionTimeMs- Execution timeurl- The URL that was called- Returns:
- An error response
-
isSuccessful
public boolean isSuccessful()Checks if the response indicates success (2xx status code).- Returns:
- true if status code is between 200 and 299
-
isClientError
public boolean isClientError()Checks if the response indicates a client error (4xx status code).- Returns:
- true if status code is between 400 and 499
-
isServerError
public boolean isServerError()Checks if the response indicates a server error (5xx status code).- Returns:
- true if status code is between 500 and 599
-
isError
public boolean isError()Checks if there was an error (network error or non-success HTTP status).- Returns:
- true if there was an error
-
isRedirect
public boolean isRedirect()Checks if the response was a redirect (3xx status code).- Returns:
- true if status code is between 300 and 399
-
bodyAsJson
Parses the response body as JSON.- Returns:
- Optional containing the JsonNode, or empty if parsing fails
-
bodyAs
Parses the response body as a specific type.- Type Parameters:
T- The type to parse to- Parameters:
clazz- The class of the type- Returns:
- Optional containing the parsed object, or empty if parsing fails
-
getJsonField
Gets a specific field from the JSON body.- Parameters:
fieldName- The field name to extract- Returns:
- Optional containing the field value as string, or empty if not found
-
hasJsonBody
public boolean hasJsonBody()Checks if the response body contains JSON content.- Returns:
- true if the content type is JSON and body is not empty
-
getHeader
Gets a specific header value (case-insensitive).- Parameters:
headerName- The header name- Returns:
- Optional containing the header value, or empty if not found
-
getLocation
Gets the Location header (for redirects).- Returns:
- Optional containing the Location header value
-
toSummary
Returns a summary of the response for logging.- Returns:
- A summary string
-
withBody
Creates a copy of this response with a different body.- Parameters:
newBody- The new body- Returns:
- A new response with the updated body
-
withError
Creates a copy of this response with a different error message.- Parameters:
newErrorMessage- The new error message- Returns:
- A new response with the updated error message
-
toString
Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components. -
hashCode
public final int hashCode()Returns a hash code value for this object. The value is derived from the hash code of each of the record components. -
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with '=='. -
statusCode
public int statusCode()Returns the value of thestatusCoderecord component.- Returns:
- the value of the
statusCoderecord component
-
headers
Returns the value of theheadersrecord component.- Returns:
- the value of the
headersrecord component
-
body
Returns the value of thebodyrecord component.- Returns:
- the value of the
bodyrecord component
-
contentType
Returns the value of thecontentTyperecord component.- Returns:
- the value of the
contentTyperecord component
-
executionTimeMs
public long executionTimeMs()Returns the value of theexecutionTimeMsrecord component.- Returns:
- the value of the
executionTimeMsrecord component
-
errorMessage
Returns the value of theerrorMessagerecord component.- Returns:
- the value of the
errorMessagerecord component
-
url
Returns the value of theurlrecord component.- Returns:
- the value of the
urlrecord component
-