Check out the full series of Questions Management tutorial here.
The API Question Service will call other Composites and Core Services to process the user request and return results based on the type of data the user wants. So before building API to get all the questions for the API Question Service, there are a few things we need to do:
The first thing we need to do is to add a CompositeQuestion object to contain the question’s content.
Of course, we must also define other objects to contain the information of the question, category, and option as follows:
Question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.questionservice.dto; import lombok.Data; @Data public class Question { private String id; private String description; private String categoryId; } |
Category:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava.questionservice.dto; import lombok.Data; @Data public class Category { private String id; private String name; } |
Option:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.questionservice.dto; import lombok.Data; @Data public class Option { private String id; private String description; private String note; private Boolean isCorrect; private String questionId; } |
CompositeQuestion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.huongdanjava.questionservice.dto; import java.util.List; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class CompositeQuestion { private String id; private String description; private Category category; private List<Option> options; } |
Since the API Question Service will call other Core and Composite Services, we will also configure the services in the application.properties file as follows:
1 |
compositequestionservice.url=http://localhost:8181 |
To handle the call to the Composite Question Service, we will create an interface called CompositeQuestionService:
1 2 3 4 5 6 7 |
package com.huongdanjava.questionservice.service; public interface CompositeQuestionService { String getServiceUrl(); } |
with the implementation of CompositeQuestionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.questionservice.service.impl; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.huongdanjava.questionservice.service.CompositeQuestionService; @Service public class CompositeQuestionServiceImpl implements CompositeQuestionService { @Value("${compositequestionservice.url}") private String compositeQuestionServiceUrl; @Override public String getServiceUrl() { return compositeQuestionServiceUrl; } } |
As you can see, in the CompositeQuestionServiceImpl class, I’ve injected information about the Composite Question Service’s base URL.
I will run this service using port 8182 so I will also add the server.port property in the application.properties file as follows:
1 |
server.port=8281 |
OK, everything is ready, now I will go to the main part of this tutorial!
I will create a controller called ApiQuestionController with the following content:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava.questionservice; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/question") public class ApiQuestionController { } |
With this declaration, I also expose APIs to the API Question Service with the request URL starting with “/question”.
Next we will inject CompositeQuestionService to use:
1 2 |
@Autowired private CompositeQuestionService compositeQuestionService; |
To build API to get all the questions, I’m going to add a method in the CompositeQuestionService class named:
1 |
Flux<CompositeQuestion> findAllQuestions(); |
with implementation as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override public Flux<CompositeQuestion> findAllQuestions() { WebClient client = WebClient.builder() .baseUrl(getServiceUrl()) .build(); WebClient.ResponseSpec responseSpec = client.get() .uri("/question/all") .retrieve(); return responseSpec.bodyToFlux(CompositeQuestion.class); } |
As you can see, here I have used the WebClient object to connect to the Composite Question Service and call the API to get all questions with the “/question/all” URI.
Next, I will add a new method in ApiQuestionController to expose a GET request “/all” to get all the questions:
1 2 3 4 |
@GetMapping("/all") public Flux<CompositeQuestion> findAllQuestions() { } |
Our task is to simply call the CompositeQuestionService’s findAllQuestions() method:
1 2 3 4 |
@GetMapping("/all") public Flux<CompositeQuestion> findAllQuestions() { return compositeQuestionService.findAllQuestions(); } |
At this point, we have completed the API to get all questions for the API Question Service. Let’s try it out!