Skip to content

Instantly share code, notes, and snippets.

@afsinka
Created February 10, 2019 10:56
Show Gist options
  • Save afsinka/53081a7c946577da4bcccfa818339897 to your computer and use it in GitHub Desktop.
Save afsinka/53081a7c946577da4bcccfa818339897 to your computer and use it in GitHub Desktop.
import io.swagger.api.ImagesApi;
import io.swagger.model.GetImagesResponse;
import io.swagger.model.Image;
import java.io.InputStream;
import java.util.Base64;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ImageController implements ImagesApi {
@Override
public ResponseEntity<GetImagesResponse> getImages() {
try {
final GetImagesResponse response = new GetImagesResponse();
final InputStream inputStream = new ClassPathResource("images/bojack.jpg").getInputStream();
final byte[] bytes = IOUtils.toByteArray(inputStream);
final String encodedImage = Base64.getEncoder().encodeToString(bytes);
final Image image = new Image();
image.setValue(encodedImage);
response.add(image);
return new ResponseEntity<GetImagesResponse>(response, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
}
throw new RuntimeException("oops!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment