Learn Microservices with Spring Boot 3 by 2023
Author:2023
Language: eng
Format: epub
Chapter 6 Starting with MiCroServiCeS
Like in standard SQL, the GROUP BY clause indicates how to sum up the values. You
can define parameters with the :param notation. Then, you annotate the corresponding
method arguments with @Param. You could also use the approach followed in the
previous chapter, with argument positionâs placeholders like ?1.
The second query is a bit special. In JPQL, you can use the constructors available in
your Java classes. What you do in this example is an aggregation based on the total score,
and you construct LeaderBoardRow objects using the two-argument constructor you
defined (which sets an empty list of badges). Keep in mind that you have to use the fully
qualified name of the class in JPQL, as shown in the source code.
Controller
While designing the Gamification domain, you agreed on a contract with the
Multiplication service. Itâll send each attempt to a REST endpoint on the gamification
side. Itâs time to build that controller. See Listing 6-15.
Listing 6-15. The GameController Class
package microservices.book.gamification.game;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import microservices.book.gamification.challenge.ChallengeSolvedDTO;
@RestController
@RequestMapping("/attempts")
@RequiredArgsConstructor
public class GameController {
private final GameService gameService;
@PostMapping
@ResponseStatus(HttpStatus.OK)
void postResult(@RequestBody ChallengeSolvedDTO dto) {
gameService.newAttemptForUser(dto);
}
}
207
Chapter 6 Starting with MiCroServiCeS
There is a REST API available on POST /attempts that accepts a JSON object
containing data about the user and the challenge. In this case, you donât need to return
any content, so you can use the ResponseStatus annotation to configure Spring to
return a 200 OK status code. Actually, this is the default behavior when a controllerâs
method returns void and has been processed without errors. In any case, itâs good to
add it explicitly for better readability. Remember that if there is an error like a thrown
exception, for example, Spring Bootâs default error handling logic will intercept it and
return an error response with a different status code.
You could also add validation to the DTO class to make sure other services donât
send invalid data to the Gamification microservice, but, for now, letâs keep it simple.
Youâll change this API in the next chapter anyway.
Exercise Donât forget to add the tests for this first controller and the next one.
You can find these tests in this chapterâs source code.
The second controller is for the leaderboard functionality and exposes a GET /leaders
method that returns a JSON array of serialized LeaderBoardRow objects. This data is
coming from the service layer, which uses the badge and score repositories to merge
usersâ scores and badges. Therefore, the presentation layer remains simple. See the code
in Listing 6-16.
Listing 6-16. The LeaderBoardController Class
package microservices.book.gamification.game;
import lombok.RequiredArgsConstructor;
import microservices.book.gamification.game.domain.LeaderBoardRow;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* This class implements a REST API for the Gamification LeaderBoard
service .
*/
@RestController
@RequestMapping("/leaders")
@RequiredArgsConstructor
208
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9867)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9757)
The Mikado Method by Ola Ellnestam Daniel Brolund(9747)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8258)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7745)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7741)
Grails in Action by Glen Smith Peter Ledbrook(7667)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7517)
Windows APT Warfare by Sheng-Hao Ma(6502)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6378)
Layered Design for Ruby on Rails Applications by Vladimir Dementyev(6248)
Blueprints Visual Scripting for Unreal Engine 5 - Third Edition by Marcos Romero & Brenden Sewell(6117)
Kotlin in Action by Dmitry Jemerov(5019)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4297)
Functional Programming in JavaScript by Mantyla Dan(4018)
Solidity Programming Essentials by Ritesh Modi(3839)
WordPress Plugin Development Cookbook by Yannick Lefebvre(3614)
Unity 3D Game Development by Anthony Davis & Travis Baptiste & Russell Craig & Ryan Stunkel(3565)
The Ultimate iOS Interview Playbook by Avi Tsadok(3532)
