- Json Dependency
<!-- https://mvnrepository.com/artifact/org.glassfish/jakarta.json -->
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<version>2.0.1</version>
</dependency>
- Jedis Dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.1</version>
</dependency>
- Setting up
set REDISHOST localhost
set REDISPORT 6379
{
"name": "Mastermind",
"pieces": {
"decoding_board": {
"total_count": 1
},
"pegs": {
"total_count": 102,
"types": [
{
"type": "code",
"count": 72
},
{
"type": "key",
"count": 30
}
]
},
"rulebook": {
"total_count": 1,
"file": "rulebook-ultimate-mastermind.pdf"
}
}
}
- we can see that 6 classes is needed for this JSON
- Therefore, 6 models are needed
- All variables need to follow the Json file naming
- The controller used
@RestController
For Json it is using rest controller not purely controller 2. The request mapping
@RequestMapping(path="/api/boardgame", consumes = MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
For Json, in the request mapping there is consume and produce 3. This part will fufil GET /api/boardgame/
@GetMapping(path="{msId}")
- all need to have getter and setter for all variables
- DecodingBoard only has total_count var therefore only 1 variable 2.Need to convert it to a Json Object using the following code
public JsonObjectBuilder toJSON(){
return Json.createObjectBuilder()
.add("totalCount", this.getTotal_count());
}
- Mastermind has 6 variables
- insertCount and updateCount is needed as stated in the workshop 3.isUpSert is based on task 3
- Pegs has total_count var and types obj
- total_count needs to be converted to JsOn obj
- types needs to be converted to Json obj
- Code
public JsonObjectBuilder toJSON(){
JsonArrayBuilder arrbld = Json.createArrayBuilder();
List<JsonObjectBuilder> listOfTypes = this.getTypes()
.stream()
.map(t -> t.toJSON())
.toList();
for (JsonObjectBuilder x : listOfTypes)
arrbld.add(x);
return Json.createObjectBuilder()
.add("total_count", this.getTotal_count())
.add("types", arrbld);
- Pieces have 4 obj decoding_board, pegs and rulebook
- All need to be converted to Json Obj
- Code
public JsonObjectBuilder toJSON(){
return Json.createObjectBuilder()
.add("decoding_board", this.getDecoding_board().toJSON())
.add("pegs", this.getPegs().toJSON())
.add("rulebook", this.getRulebook().toJSON());
}
- Rulebook has total_count var and file obj
- All need to be converted to Json Obj
- Code
public JsonObjectBuilder toJSON(){
return Json.createObjectBuilder()
.add("total_count", this.getTotal_count())
.add("file", this.getFile());
}
- Types have 2 var type and count
- Both need to be converted to Json Obj
- Code
public JsonObjectBuilder toJSON(){
return Json.createObjectBuilder()
.add("type", this.getType())
.add("count", this.getCount());
}
- This is a service hence @Service is needed
- KIV on what is a service