This repo contains working code for a Url Image Count service (SpringBoot with Java8).
There are two different implementations provided in this application.
- In this implementation the application provides a REST API (/v1/imagecount) that will accept a list of live URLs as JSON input. This call would return a Job Id immediately after invoked.
- The application’s back-end would then get the html contents for each URL and then count the total number of images found in that content by parsing the <img src="" > tag using regular expression. This would be done asynchronously.
- The application also provides another REST API (v1/imagecount/jobId/{jobId}) with a Job ID as path parameter.
- This API will return a JSON response with the number of images found in the contents of each URL supplied for that job id.
- The total image count for each url content will be displayed if that particular url request is completed
- Otherwise the status 'Pending' will be displayed
- The urls in the request payload could be any live urls.
- This implementation uses Executor interface for multithreading and CompletableFuture for asynchronous programming for processing the urls, parsing and counting the images.
- This implementation provides the REST endpoint (/v1/imagecount/demourl) that will accept a list of demo urls as JSON input. This call would also return a Job Id immediately after invoked.
- For the purpose of demonstration this application also contains REST API for providing the demo Url contents. The contents available thro' the uri's '/v1/url1/image, /v1/url2/image and /v1/url3/image'. The response of these urls are not html contents. They are JSON response with tags.
- The same REST endpoint (v1/imagecount/jobId/{jobId}) with a Job ID as path parameter can be used to retrieve the job details.
- This will also return a JSON response with the number of images found in the contents of each URL supplied for that job id.
- The total image count for each url content will be displayed if that particular url request is completed
- Otherwise the status 'Pending' will be displayed
- The urls in the request payload could be any of the three demo (url1, url2, url3) urls.
- This one uses @Async of Spring for url processing and the image count.
- Multithreaded & Asynchronous Spring Boot and Java 8 based REST implementation
- Layered approach
- Dependency Injection using Spring
- Input data and Service validation
- Testing
- Unit Testing
- Curl
- Postman
Description | Tool/Framework |
---|---|
Java version | JDK 1.8 |
JAX-RS Implementation | Spring Boot |
IDE | IntelliJ IDEA 14 |
Build tool | Gradle |
Platform | iOS |
Resource Type | Resource URI | HTTP Method |
---|---|---|
Create a Job (for Live Urls) | /v1/imagecount | POST |
Create a Job (for Demo Urls) | /v1/imagecount/demourl | POST |
Get a Job Detail | /v1/imagecount/jobId/{jobId} | GET |
Get Url1 content | /v1/url1/image | GET |
Get Url2 content | /v1/url2/image | GET |
Get Url3 content | /v1/url3/image | GET |
- Clone/fork this Repo and open/import this Project into IntelliJ or Eclipse
- Open a Terminal window and go to the project directory, 'url-imagecount-service'
- Build/Package the project using 'gradle build' command (use gradle build -x test to skip the test)
- Start SpringBoot application using 'java -jar build/libs/imagecount-service-0.1.0.jar' command
- Test the server by http://localhost:8080/v1/imagecount/jobId/1. This may display {"errorMessage":"JobId 1 is not found"}, as you have not created any Job already.
- For a quick deployment you can use the shared imagecount-service-0.1.0.jar file, in case you didn't have time or ran into any issues.
- Simply go to the root folder and run the command 'java -jar build/libs/imagecount-service-0.1.0.jar'
There are 2 ways you can consume these resources. They are by using Postman, and Curl command.
The resources can be consumed by using below Postman collection
If the JobId is not available then an error message will be displayed
{
"errorMessage": "JobId 11 is not found"
}
We can test the resources using below Curl command. The data files are available in the root folder [liveurl_requestX.json or demourl_requestX.json]
Please make sure that the Application is running before running this command.
- Command: You can try with different data file (demourl_request2.json, demourl_request3.json)
curl -H "Content-Type:application/json" -X POST -d @demourl_request1.json http://localhost:8080/v1/imagecount/demourl
- response
{ "jobId":"2", "imageCountUrls":[ { "url":"http://localhost:8080/v1/url1/image", "imageCount":"Pending" }, { "url":"http://localhost:8080/v1/url2/image", "imageCount":"Pending" } ], "_links":{ "self":{ "href":"http://localhost:8080/v1/imagecount" }, "get":{ "href":"http://localhost:8080/v1/imagecount/jobId/2" }, "update":{ "href":"http://localhost:8080/v1/imagecount/jobId/2" }, "delete":{ "href":"http://localhost:8080/v1/imagecount/jobId/2" } } }
- Command:
curl -H "Content-Type:application/json" -X GET http://localhost:8080/v1/imagecount/jobId/1
- Response
{ "jobId": "1", "imageCountUrls": [ { "url": "http://localhost:8080/v1/url1/image", "imageCount": "60" }, { "url": "http://localhost:8080/v1/url2/image", "imageCount": "130" } ], "_links": { "self": { "href": "http://localhost:8080/v1/imagecount/jobId/1" }, "update": { "href": "http://localhost:8080/v1/imagecount/jobId/1" }, "delete": { "href": "http://localhost:8080/v1/imagecount/jobId/1" } }
The unit tests are added mainly for Controller, and Service layers. These tests will be run as part of the 'gradle build' command