Last active
September 9, 2023 16:28
-
-
Save mayankchoubey/6cd6cbe2272372cfa57e7b2af0fdb1f4 to your computer and use it in GitHub Desktop.
SpringBoot Webflux - QR generator API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| server.port=3000 | |
| server.ssl.certificate=/Users/mayankc/Work/source/certs/cert.pem | |
| server.ssl.certificate-private-key=/Users/mayankc/Work/source/certs/key.pem |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package webfluxqr; | |
| import org.springframework.boot.SpringApplication; | |
| import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| import org.springframework.context.annotation.Bean; | |
| import org.springframework.core.io.ClassPathResource; | |
| import org.springframework.web.reactive.config.EnableWebFlux; | |
| @EnableWebFlux | |
| @SpringBootApplication | |
| public class QrApplication { | |
| public static void main(String[] args) { | |
| SpringApplication.run(QrApplication.class, args); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package webfluxqr; | |
| import org.springframework.web.bind.annotation.RequestMapping; | |
| import org.springframework.web.bind.annotation.PostMapping; | |
| import org.springframework.web.bind.annotation.RequestBody; | |
| import org.springframework.web.bind.annotation.ResponseStatus; | |
| import org.springframework.http.ResponseEntity; | |
| import org.springframework.http.HttpStatus; | |
| import org.springframework.http.HttpHeaders; | |
| import org.springframework.web.bind.annotation.RestController; | |
| import reactor.core.publisher.Flux; | |
| import reactor.core.publisher.Mono; | |
| import webfluxqr.QrGenerator; | |
| @RestController | |
| @RequestMapping("/") | |
| public class QrController { | |
| @PostMapping("/qr") | |
| @ResponseStatus(HttpStatus.OK) | |
| public ResponseEntity handleRequest(@RequestBody QrRequest qrRequest) { | |
| if(qrRequest.getUrlToEmbed() == null) { | |
| return new ResponseEntity<>(HttpStatus.BAD_REQUEST); | |
| } | |
| try { | |
| HttpHeaders httpHeaders = new HttpHeaders(); | |
| httpHeaders.add(HttpHeaders.CONTENT_TYPE, "image/png"); | |
| return new ResponseEntity<byte[]>( | |
| QrGenerator.generateQR(qrRequest.getUrlToEmbed(), 512, 512), | |
| httpHeaders, | |
| HttpStatus.OK); | |
| } catch (Exception e) { | |
| return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package webfluxqr; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import com.google.zxing.BarcodeFormat; | |
| import com.google.zxing.WriterException; | |
| import com.google.zxing.client.j2se.MatrixToImageConfig; | |
| import com.google.zxing.client.j2se.MatrixToImageWriter; | |
| import com.google.zxing.common.BitMatrix; | |
| import com.google.zxing.qrcode.QRCodeWriter; | |
| public class QrGenerator { | |
| public static byte[] generateQR(String text, int width, int height) throws WriterException, IOException { | |
| QRCodeWriter qrCodeWriter = new QRCodeWriter(); | |
| BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height); | |
| ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream(); | |
| MatrixToImageConfig con = new MatrixToImageConfig() ; | |
| MatrixToImageWriter.writeToStream(bitMatrix, "PNG", pngOutputStream, con); | |
| byte[] pngData = pngOutputStream.toByteArray(); | |
| return pngData; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package webfluxqr; | |
| public class QrRequest { | |
| private String urlToEmbed; | |
| public String getUrlToEmbed() { | |
| return this.urlToEmbed; | |
| } | |
| public void setUrlToEmbed(String urlToEmbed) { | |
| this.urlToEmbed = urlToEmbed; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment