jsonBody with byte[ ] - Hi. I need to build respon...
# wiremock-java
s
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
Do you want the response to be a binary file in its entirety, or do you want a byte array wrapped in JSON?
s
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
I think a JSON array of numbers should get deserialised to a byte array. Have you tried this?
s
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
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
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')
118 Views