Learn Microservices with Spring Boot 3 by 2023

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



Copyright Disclaimer:
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.