https://linen.dev logo
#wiremock-java
Title
# wiremock-java
s

Seweryn

12/21/2023, 10:33 AM
jsonBody with byte[] - Hi. I need to build response which servers byte[] (file) like that:
Copy code
"response": {
  "status": 200,
  "headers": {
    "content-type": "application/json"
  },
  "jsonBody": {
    "content": <byte[]>
  },
  "transformers": [
    "json-body-transformer"
  ]
}
Does anyone know how to do it or where to find some hints ? Thank you so much and have a lovely Christmas time !
t

Tom

12/21/2023, 11:03 AM
Do you want the response to be a binary file in its entirety, or do you want a byte array wrapped in JSON?
s

Seweryn

12/21/2023, 11:10 AM
Copy code
public class FileContent {

    @NotNull
    @JsonProperty
    private final byte[] content;
This is what I need to mock
Copy code
ResponseEntity<FileContent> response = restTemplate.exchange(apiURL, HttpMethod.GET, request, FileContent.class);
t

Tom

12/21/2023, 9:17 PM
I think a JSON array of numbers should get deserialised to a byte array. Have you tried this?
s

Seweryn

12/21/2023, 9:18 PM
getting that error
Could not extract response: no suitable HttpMessageConverter found for response type [class com.mydomain.FileContent] and content type [application/octet-stream]
t

Tom

12/21/2023, 9:19 PM
Try adding a response header
Content-Type
of
application/json
(The error suggests at least, that you’ve either not got one or it’s not one your framework recognises)
s

Seweryn

12/21/2023, 9:21 PM
Adding 'application/json' causing that error
org.springframework.web.client.RestClientException: Error while extracting response for type [class com.mydomain.FileContent] and content type [application/json]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('%' (code 37)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
16 Views