Check out the full series of Questions Management tutorial here.
Before creating the API add new option for the API Option Service, there are a few things we need to do:
The first thing we need to do is add an Option object to contain the information of an option.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.huongdanjava.optionservice.dto; import lombok.Data; @Data public class Option { private String id; private String description; private String note; private Boolean isCorrect; private String questionId; } |
Since API Option Service will call Core Option Service and Composite Option Service, we will also configure information about these services in the application.properties file as follows:
1 2 |
coreoptionservice.url=http://localhost:8083 compositeoptionservice.url=http://localhost:8183 |
To handle the call to Composite Option Service, I will create an interface named CompositeOptionService:
1 2 3 4 5 6 7 |
package com.huongdanjava.optionservice.service; public interface CompositeOptionService { String getServiceUrl(); } |
with the implementation of CompositeOptionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.optionservice.service.impl; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.huongdanjava.optionservice.service.CompositeOptionService; @Service public class CompositeOptionServiceImpl implements CompositeOptionService { @Value("${compositeoptionservice.url}") private String compositeOptionServiceUrl; @Override public String getServiceUrl() { return compositeOptionServiceUrl; } } |
To handle the call to the Core Option Service, I will create an interface called CoreOptionService:
1 2 3 4 5 6 7 |
package com.huongdanjava.optionservice.service; public interface CoreOptionService { String getServiceUrl(); } |
with implementation is CoreOptionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.huongdanjava.optionservice.service.impl; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.huongdanjava.optionservice.service.CoreOptionService; @Service public class CoreOptionServiceImpl implements CoreOptionService { @Value("${coreoptionservice.url}") private String coreOptionServiceUrl; @Override public String getServiceUrl() { return coreOptionServiceUrl; } } |
I will run this service using port 8283 so I will also add the server.port property in the application.properties file as follows:
1 |
server.port=8283 |
OK, everything is ready, now I will go to the main part of this tutorial!
I will create a new controller called ApiOptionController with the following content:
1 2 3 4 5 6 7 8 9 10 |
package com.huongdanjava.optionservice; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/option") public class OptionController { } |
With this declaration, I also expose APIs for the API Option Service with the request URL starting with “/option”.
Next we will inject CompositeOptionService and CoreOptionService in to use:
1 2 3 4 5 |
@Autowired private CoreOptionService coreOptionService; @Autowired private CompositeOptionService compositeOptionService; |
To build the API add new option, I’m going to add a new method in the CompositeOptionService class named:
1 |
Mono<Option> addNewOption(Option option); |
with the implementation of the CompositeOptionServiceImpl class is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Override public Mono<Option> addNewOption(Option option) { WebClient client = WebClient.builder() .baseUrl(getServiceUrl()) .build(); WebClient.ResponseSpec responseSpec = client.post() .uri("/option/add") .body(BodyInserters.fromObject(option)) .retrieve(); return responseSpec.bodyToMono(Option.class); } |
As you can see, here I have used the WebClient object to connect to the Composite Option Service and call the API add a new option of this service with the “/option/add” URI.
Next, I will add a new method in ApiOptionController to expose a POST request “/option/add” to add a new option:
1 2 3 4 |
@PostMapping("/add") public Mono<ResponseEntity<Option>> addNewOption(@RequestBody Option option) { } |
Our task is to simply call the CompositeOptionService addNewOption() method:
1 2 3 4 |
@PostMapping("/add") public Mono<ResponseEntity<Option>> addNewOption(@RequestBody Option option) { return compositeOptionService.addNewOption(option).map(o -> ResponseEntity.ok(o)); } |
At this point, we have completed the API to add a new option for API Option Service. Let’s try it out!