[JAVA] Call API [Preparation]

Call API

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]

Preparation

First, prepare the necessary model, Json conversion, and self-made Exception.

--Model: Box for request / response --Json conversion: Convert request / response to Json for API communication --Exception: Create an Exception according to the result and prepare for branching of processing

model

Getter / Setter is omitted

request.java


package model;

public class RequestDto {
	
	/**Outer frame*/
	private RequestBody request;
}

Request contents.java


package model;

public class RequestBody {
	
	/** id */
	private String id;
	/**name*/
	private String name;
	/**Contents*/
	private String info;
}

response.java


package model;

public class ResponseDto {

	/**Processing result*/
	private String resultCode;
	/**Contents*/
	private ResponseBody body;
}

Response contents.java


package model;

public class ResponseBody {
	
	/**Result content*/
	private String info;
	/**comment*/
	private String comment;
}

Json conversion

JsonUtil.java


package json;

import model.RequestDto;
import model.ResponseBody;
import model.RequestBody;
import model.ResponseDto;

public class JsonUtil {
	
	/**
	 *Convert request DTO to Json
	 * 
	 * @param request request DTO
	 * @return Json Converted string
	 */
	public static String requestDtoToJson(RequestDto request) {
		
		RequestBody body = request.getRequest();
		
		String json = 
				"{" + 
					"\"request\": {" + 
						"\"id\": " + body.getId() + "," + 
						"\"name\": " + body.getName() + "," + 
						"\"info\": " + body.getInfo() + 
					"}" + 
				"}";
		
		return json;
	}
	
	/**
	 *Convert response Json to DTO
	 * 
	 * @param json response json
	 * @return Converted DTO
	 */
	public static ResponseDto responseJsonToDto(String json) {
		
		json = 
				"{" +
					"\"resultCode\": 0," +
					"\"body\": {" +
						"\"info\":The result is this('ω')," +
						"\"comment\":It's a comment('ω')" +
					"}" +
				"}";
		
		ResponseBody body = new ResponseBody();
		body.setInfo(getContent(json, "info"));
		body.setComment(getContent(json, "comment"));
		ResponseDto response = new ResponseDto();
		response.setBody(body);
		response.setResultCode(getContent(json, "resultCode"));
		
		return response;
	}
	
	/**
	 *Extract information from Json string
	 * <p>
	 *XXX It's useless if the content contains a resultCode character string or a comma, isn't it?
	 * 
	 * @param json Json string
	 * @param content Parameter name to be cut out
	 * @return Cut out value
	 */
	private static String getContent(String json, String content) {
		return json.split("\"" + content + "\": ")[1].split(",")[0].split("}")[0].trim();
	}
}

The response is focused on ease of use, so this time I've re-entered a fixed value. Well, I think there is a library, so maybe this isn't really necessary?

Self-made Exception

Basis Exception.java


package exception;

public class NantokaApiException extends Exception{

	private static final long serialVersionUID = 1L; 
	
	public NantokaApiException(String msg){
		super(msg);
	}
}

Exception that summarizes unexpected error systems.java


package exception;

public class NantokaApiUnknownException extends NantokaApiException {

	private static final long serialVersionUID = 1L; 
	
	public NantokaApiUnknownException(String msg){
		super(msg);
	}
}

Exception when there is API processing result but it is an error result.java


package exception;

public class NantokaApiBadResultException extends NantokaApiException {

	private static final long serialVersionUID = 1L; 
	
	public NantokaApiBadResultException(String msg){
		super(msg);
	}
}

So it was a preparation Next time I will use these guys to hit the API

Recommended Posts

Call API [Preparation]
Call API [Call]
Call API [Handling]
Call API [Core Edition]
Call TensorFlow Java API from Scala
Rails API
Call GitHub's Rest API from Java's Socket API
Call GitHub API from Java Socket API part2
Call the Windows Notification API in Java
RSpec preparation