It is divided into the following four parts. (Forgive me for the XX edition) Call API [Preparation] Call API [Core] Call API [Handling] Call API [Call]
Up to the last time, I was able to make it so that if you call Facade, it will do all the API related processing. Let's call Facade
service.java
package service;
import exception.NantokaApiBadResultException;
import exception.NantokaApiUnknownException;
import facade.Facade;
import model.RequestBody;
import model.RequestDto;
import model.ResponseBody;
import model.ResponseDto;
public class Service {
/** APIFacade */
private static Facade facade = new Facade();
public static void main(String[] args) {
System.out.println(service(""));
}
public static String service(String str) {
//logic
RequestBody body = new RequestBody();
body.setId("28");
body.setName("Somehow");
body.setInfo("I'm hungry");
RequestDto request = new RequestDto();
request.setRequest(body);
try {
//API execution
ResponseDto response = facade.nantokaApiFacade(request);
ResponseBody responseBody = response.getBody();
return responseBody.getInfo() + responseBody.getComment();
} catch (NantokaApiUnknownException e) {
return e.getMessage();
} catch (NantokaApiBadResultException e) {
return e.getMessage();
}
}
}
The error is simple and nice It is possible to combine the case of Exception in Facade and the case where the processing result is null into one error. After that, I am taking an error when the result is not normal If you want to divide the processing according to the type of result, you can handle it by increasing Exception and branching.
★ Client (core layer) just hits the API ★ Make Exceptions spit out as you like in Facade (handling layer) ★ Implemented processing when branching in Service (call layer)
Isn't it pretty beautiful!
Recommended Posts