The Voter Spring Boot Service is a RESTful Web Service, backed by MongoDB. The Voter service exposes several HTTP API endpoints, listed below. API users can review a static list candidates (based on the 2016 US Presidential Election), submit a vote, view voting results, and inspect technical information about the running service. API users can also create random voting data by calling the /simulation
endpoint.
The Voter service is dependent on the Candidate Service to supply a list of candidates. The Candidate service is called by the Voter service when either the /candidates
or /simulation
endpoints are called.
The Voter service requires MongoDB to be running locally, on port 27017
. The Voter service also required the Candidate service to be running locally on 8097
. To clone, build, test, and run the Voter service, as a JAR file, locally:
git clone https://github.com/garystafford/voter-service.git
cd voter-service
./gradlew clean cleanTest build
java -jar build/libs/voter-service-0.2.0.jar
By default, the service runs on localhost
, port 8099
. By default, the service looks for MongoDB on localhost
, port 27017
.
Purpose | Method | Endpoint |
---|---|---|
Create Random Sample Data | GET | /simulation |
List Candidates | GET | /candidates |
Submit Vote | POST | /votes |
View Voting Results | GET | /results |
View Total Votes | GET | /results/votes |
View Winner(s) | GET | /winners |
View Winning Vote Count | GET | /winners/votes |
Service Info | GET | /info |
Service Health | GET | /health |
Service Metrics | GET | /metrics |
Other Spring Actuator endpoints | GET | /actuator , /mappings , /env , /configprops , etc. |
Other HATEOAS endpoints for /votes |
Various | DELETE, PATCH, PUT, page sort, size, etc. |
The HAL Browser API browser for the hal+json
media type is installed alongside the service. It can be accessed at http://localhost:8099/actuator/
.
Submitting a new vote, requires an HTTP POST
request to the /votes
endpoint, as follows:
HTTPie
http POST http://localhost:8099/votes \
candidate="Jill Stein (Green Party)"
cURL
curl -X POST \
-H "Content-Type: application/json" \
-d '{ "candidate": "Jill Stein (Green Party)" }' \
"http://localhost:8099/votes"
wget
wget --method POST \
--header 'content-type: application/json' \
--body-data '{ "candidate": "Jill Stein (Green Party)" }' \
--no-verbose \
--output-document - http://localhost:8099/votes
Using HTTPie command line HTTP client.
http http://localhost:8099/candidates
{
"candidates": [
"Chris Keniston (Veterans Party)",
"Darrell Castle (Constitution Party)",
"Donald Trump (Republican Party)",
"Gary Johnson (Libertarian Party)",
"Hillary Clinton (Democratic Party)",
"Jill Stein (Green Party)"
]
}
http http://localhost:8099/simulation
{
"message": "random simulation data created"
}
http http://localhost:8099/results
{
"results": [
{
"candidate": "Jill Stein (Green Party)",
"votes": 18
},
{
"candidate": "Darrell Castle (Constitution Party)",
"votes": 17
},
{
"candidate": "Chris Keniston (Veterans Party)",
"votes": 16
},
{
"candidate": "Gary Johnson (Libertarian Party)",
"votes": 15
},
{
"candidate": "Donald Trump (Republican Party)",
"votes": 7
},
{
"candidate": "Hillary Clinton (Democratic Party)",
"votes": 7
}
]
}
http http://localhost:8099/results/votes
{
"votes": 80
}
http http://localhost:8099/winners
{
"results": [
{
"candidate": "Jill Stein (Green Party)",
"votes": 18
}
]
}
http http://localhost:8099/winners/votes
{
"votes": 18
}
http POST http://localhost:8099/votes candidate="Jill Stein (Green Party)"
{
"_links": {
"self": {
"href": "http://localhost:8099/votes/5888605326b6f40371a1d016"
},
"vote": {
"href": "http://localhost:8099/votes/5888605326b6f40371a1d016"
}
},
"candidate": "Jill Stein (Green Party)"
}
The project's source code is continuously built and tested on every commit to GitHub, using Travis CI. If all unit tests pass, the resulting Spring Boot JAR is pushed to the artifacts
branch of the voter-service-artifacts GitHub repository. The JAR's filename is incremented with each successful build (i.e. voter-service-0.2.10.jar
).
The Voter service includes several Spring Boot Profiles, in a multi-profile YAML document: src/main/resources/application.yml
. The profiles are default
, docker-development
, docker-production
, and aws-production
. You will need to ensure your MongoDB instance is available at that host
address and port of the profile you choose, or you may override the profile's properties.
server:
port: 8099
spring:
data:
mongodb:
host: localhost
port: 27017
database: voters
logging:
level:
root: INFO
info:
java:
source: ${java.version}
target: ${java.version}
management:
info:
git:
mode: full
build:
enabled: true
services:
candidates:
host: localhost
port: 8097
---
spring:
profiles: docker-development
data:
mongodb:
host: mongodb
services:
candidates:
host: candidates
---
spring:
profiles: aws-production
data:
mongodb:
host: 10.0.1.6
logging:
level:
root: WARN
management:
info:
git:
enabled: false
build:
enabled: false
endpoints:
sensitive: true
enabled: false
info:
enabled: true
health:
enabled: true
services:
candidates:
host: candidates
---
spring:
profiles: docker-production
data:
mongodb:
host: mongodb
logging:
level:
root: WARN
management:
info:
git:
enabled: false
build:
enabled: false
endpoints:
sensitive: true
enabled: false
info:
enabled: true
health:
enabled: true
services:
candidates:
host: candidates
All profile property values may be overridden on the command line, or in a .conf
file. For example, to start the Voter service with the aws-production
profile, but override the mongodb.host
value with a new host address, you might use the following command:
java -jar <name_of_jar_file> \
--spring.profiles.active=aws-production \
--spring.data.mongodb.host=<new_host_address>
-Djava.security.egd=file:/dev/./urandom
How to create sample voter data using both services:
- Create Sample Candidate List:
http localhost:8097/simulation
- Create Sample Voter Data using Candidate List:
http localhost:8099/simulation
- View Sample Voter Data:
http localhost:8099/results