How do you use Spring WebClient as a file download passthru - java

I am trying to use WebClient to download a file from a external service and return it to the client. In the Rest Controller, I have the following endpoint:
#GetMapping(value = "/attachment/{fileId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public Flux<byte[]> get(#PathVariable String fileId) {
return this.webClient.get()
.uri(builder ->
builder.scheme("https")
.host("external-service.com")
.path("/{fileId}")
.build(fileId)
).attributes(clientRegistrationId("credentials"))
.accept(MediaType.APPLICATION_OCTET_STREAM)
.retrieve()
.bodyToFlux(byte[].class);
}
When I try to hit the endpoint, I get the following error:
Exception Class:class org.springframework.http.converter.HttpMessageNotWritableException
Stack Trace:org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class org.springframework.web.servlet.mvc.method.annotation.ReactiveTypeHandler$CollectedValuesList] with preset Content-Type 'null'
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:317)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:181)
I have tried returning Flux<DataBuffer> instead, but getting the same error message.
I'm using spring-boot-starter-web and spring-boot-starter-webflux version 2.2.4.RELEASE. It is running on a tomcat server.
What am I missing?
Edit:
I'm trying to stream the result to the client without buffering the whole file into memory.

This works for me. Just change from Flux to Mono. Seem there is no converter for Flux<byte[]>
#GetMapping(value = "/attachment/{fileId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public Mono<byte[]> get(#PathVariable String fileId) {
return this.webClient.get()
.uri(builder ->
builder.scheme("https")
.host("external-service.com")
.path("/{fileId}")
.build(fileId)
).attributes(clientRegistrationId("credentials"))
.accept(MediaType.APPLICATION_OCTET_STREAM)
.retrieve()
.bodyToMono(byte[].class);
}

Related

How to get binary data from RestTemplate or WebClient in spring boot?

I need to call an API from my microservice which returns binary file data which i have to return back to the requester system
I tried using below code:
#RequestMapping(value="/getDocument", method=RequestMethod.POST)
private ResponseEntity<byte[]> receiveFile(){
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("mobile","XXXXXXXXX9");
WebClient webClient = WebClient.create();
ResponseEntity<byte[]> apiResponse = webClient.post()
.uri(new URI("https://api.myapp.net.in/getDocument"))
.header("username", "xyz")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
//.accept(MediaType.parseMediaType("application/pdf"))
.body(BodyInserters.fromFormData(map))
.retrieve()
.toEntity(byte[].class)
.block();
return apiResponse;
}
When i execute the above code and try to consume from Postman i am getting error as unable to open file but when i directly call the https://api.myapp.net.in/getDocument API via Postman i am able to download the pdf file properly.
Please let me know where am i going wrong.

How to input attribute into the webtestclient

I have a controller in which it fetches a value from the attribute map.
The code responsible for fetching something in the attributes is this:
Map<String, Object> memberClaims = (Map<String, Object>) request.getAttribute("token");
And in my test this is how I've wired it up:
#Test
public void shouldReturn200()
{
webTestClient.post()
.uri(URL)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(Mono.just(order()), Token.class)
.attribute("token", "123abc")
.exchange()
.expectStatus()
.is2xxSuccessful();
}
But the .attribute seems to not have any effect. I've debugged and can see that the token map is not in the MockHttpServletRequest.. thus the test returns a 500 server response.. null pointer.
Anyone know how i can add attributes to the mock request?
You must be aware that .attribute(String, String) is not intended to send parameters from the client along with the request. It is used to put additional attributes to a request when it is already on server side.
An example would be if you have an interceptor in Spring adding further information after grabbing the request an forwarding the request afterwards. This is well explained here:
To send 'attributes' from the client side you have to add them as parameters as in this example:
var respSpec = webTestClient.post()
.uri( uriBuilder - > uriBuilder
.scheme("http")
.host("localhost")
.port("10080")
.path("/endpoint")
.queryParam("requestId", requestId)
.queryParam("aUrl", aUrl)
.build())
.header(apiSecretHeader, secret)
.header("HEADER_1", "header value 1")
.header("HEADER_2", "header value 2")
.exchange();
The REST endpoint could be defined this way:
#PostMapping(value = "/endpoint", produces = {MediaType.APPLICATION_JSON_VALUE})
#ResponseStatus(HttpStatus.ACCEPTED)
public ResponseEntity<?> myEndpoint(#RequestParam final String requestId, #NonNull #RequestParam(name = "url") final URL aUrl)

Spring webflux WebClient post a file to a client

I'm trying to figure out how to write a method to simply send a file from a webflux controller to a 'regular' controller.
I'm constantly getting a common error back, but nothing I've tried has resolved it.
The method I'm sending the file from:
#GetMapping("process")
public Flux<String> process() throws MalformedURLException {
final UrlResource resource = new UrlResource("file:/tmp/document.pdf");
MultiValueMap<String, UrlResource> data = new LinkedMultiValueMap<>();
data.add("file", resource);
return webClient.post()
.uri(LAMBDA_ENDPOINT)
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(data))
.exchange()
.flatMap(response -> response.bodyToMono(String.class))
.flux();
}
I'm consuming it in a AWS Lambda with the following endpoint:
#PostMapping(path = "/input", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<List<?>> input(#RequestParam("file") MultipartFile file) throws IOException {
final ByteBuffer byteBuffer = ByteBuffer.wrap(file.getBytes());
//[..]
return new ResponseEntity<>(result, HttpStatus.OK);
}
But I'm constantly just getting:
{
"timestamp":1549395273838,
"status":400,
"error":"Bad Request",
"message":"Required request part 'file' is not present",
"path":"/detect-face"
}
back from the lambda;
Have I just setup the sending of the file incorrectly, Or do I need to configure something on the API Gateway to allow the request parameters in?
This was a interesting one for me. As I'm using a lambda function on the receiving end, and making use of aws-serverless-java-container-spring I actually had to declare the MultipartResolver manually.
The code in my question worked correctly once I added
#Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
to my configuration.
Maybe someone will stumble on this and find it useful.

Spring 5 Webclient Handle 500 error and modify the response

I have a Spring app acting as a passthrough from one app to another, making a http request and returning a result to the caller using WebClient. If the client http call returns a 500 internal server error I want to be able to catch this error and update the object that gets returned rather than re-throwing the error or blowing up the app.
This is my Client:
final TcpClient tcpClient = TcpClient.create()
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
.doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(readTimeout))
.addHandlerLast(new WriteTimeoutHandler(writeTimeout)));
this.webClient = WebClient.builder()
.clientConnector(new ReactorClientHttpConnector(HttpClient.from(tcpClient)))
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.filter(logRequest())
.filter(logResponse())
.filter(errorHandler())
.build();
And this is my error handler. I've commented where I want to modify the result, where ResponseDto is the custom object that is returned from the client call happy path
public static ExchangeFilterFunction errorHandler(){
return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
ResponseDto resp = new ResponseDto();
if(nonNull(clientResponse.statusCode()) && clientResponse.statusCode().is5xxServerError()){
//this is where I want to modify the response
resp.setError("This is the error");
}
//not necessarily the correct return type here
return Mono.just(clientResponse);
});
}
How can I achieve this? I can't find any tutorials or any information in the docs to help explain it.
Disclaimer, I'm new to webflux. We're only starting to look at reactive programming

API call with Java + STS returning "Content type 'application/octet-stream' not supported"

I am working on part of an API, which requires making a call to another external API to retrieve data for one of its functions. The call was returning an HTTP 500 error, with description "Content type 'application/octet-stream' not supported." The call is expected to return a type of 'application/json."
I found that this is because the response received doesn't explicitly specify a content type in its header, even though its content is formatted as JSON, so my API defaulted to assuming it was an octet stream.
The problem is, I'm not sure how to adjust for this. How would I get my API to treat the data it receives from the other API as an application/json even if the other API doesn't specify a content type? Changing the other API to include a contenttype attribute in its response is infeasible.
Code:
The API class:
#RestController
#RequestMapping(path={Constants.API_DISPATCH_PROFILE_CONTEXT_PATH},produces = {MediaType.APPLICATION_JSON_VALUE})
public class GetProfileApi {
#Autowired
private GetProfile GetProfile;
#GetMapping(path = {"/{id}"})
public Mono<GetProfileResponse> getProfile(#Valid #PathVariable String id){
return GetProfile.getDispatchProfile(id);
}
The service calling the external API:
#Autowired
private RestClient restClient;
#Value("${dispatch.api.get_profile}")
private String getDispatchProfileUrl;
#Override
public Mono<GetProfileResponse> getDispatchProfile(String id) {
return Mono.just(id)
.flatMap(aLong -> {
MultiValueMap<String, String> headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
return restClient.get(getDispatchProfileUrl, headers);
}).flatMap(clientResponse -> {
HttpStatus status = clientResponse.statusCode();
log.info("HTTP Status : {}", status.value());
return clientResponse.bodyToMono(GetProfileClientResponse.class);
// the code does not get past the above line before returning the error
}).map(GetProfileClientResponse -> {
log.debug("Response : {}",GetProfileClientResponse);
String id = GetProfileClientResponse.getId();
log.info("SubscriberResponse Code : {}",id);
return GetProfileResponse.builder()
// builder call to be completed later
.build();
});
}
The GET method for the RestClient:
public <T> Mono<ClientResponse> get(String baseURL, MultiValueMap<String,String> headers){
log.info("Executing REST GET method for URL : {}",baseURL);
WebClient client = WebClient.builder()
.baseUrl(baseURL)
.defaultHeaders(httpHeaders -> httpHeaders.addAll(headers))
.build();
return client.get()
.exchange();
}
One solution I had attempted was setting produces= {MediaType.APPLICATION_JSON_VALUE} in the #RequestMapping of the API to produces= {MediaType.APPLICATION_OCTET_STREAM_VALUE}, but this caused a different error, HTTP 406 Not Acceptable. I found that the server could not give the client the data in a representation that was requested, but I could not figure out how to correct it.
How would I be able to treat the response as JSON successfully even though it does not come with a content type?
Hopefully I have framed my question well enough, I've kinda been thrust into this and I'm still trying to figure out what's going on.
Are u using jackson library or jaxb library for marshalling/unmarshalling?
Try annotating Mono entity class with #XmlRootElement and see what happens.

Categories