Christophe Labouisse bio photo

Christophe Labouisse

Freelance Java expert, Docker enthusiast

Email Twitter Google+ LinkedIn Github Stackoverflow Hopwork

Following my post on Handling videos with RESTX here is the client counterpart. I have server handling both classical REST calls and video operations. On the client side I already have the rest client implemented using Spring Android wrapped by Android Annotations. As I did on the server side I want to use the same system to handle both REST calls and video transfers.

Using Android annotations, the rest client will look like this:

@Rest(converters = {MappingJackson2HttpMessageConverter.class})
@Accept(MediaType.APPLICATION_JSON)
public interface MyRestClient extends RestClientRootUrl {

    // Other methods omitted

    @Put("/videos")
    String addVideo(InputStream videoStream);

    @Get("/videos/{videoId}")
    @Accept("video/mp4")
    File getVideo(String videoId);
}

The issue I face with this naive implementation is the same I faced on the server side: Jackson is trying to process the video stream. The solution I used was to create a custom HttpMessageConverter for the video stream.

I had a small issue here with the readInternal method. Initially I wanted to pass the http stream directly but it didn’t work as the http stream got closed at some point before I had time to use it. So I eventually implement a workaround by saving the video to a file.

Then in the rest client I have to add my converter:

@Rest(converters = {VideoHttpMessageConverter.class,
                    MappingJackson2HttpMessageConverter.class})
@Accept(MediaType.APPLICATION_JSON)
public interface MyRestClient extends RestClientRootUrl {
    // Not change to the class body
}

Overview