본문으로 바로가기

Open API 연동 Controller 로직 공유 합니다.

category JAVA 2017. 10. 23. 15:47
반응형

제가 개발했던 프로젝트 내용 중 Controller 내용 입니다.

아래 코드 중 핵심은 RequestMapping과 @RequestBody String body 내용이 아닐까 생각합니다.

아래 코드의 내용은 서버측 내용입니다.(클라이언트의 내용은 다른 항목을 참고 하시기 바랍니다.)



@Controller

public class ApiController {


@Resource(name = "makeInfoService")

private MakeInfoService makeInfoService;


@Autowired

private SyncService syncService;


private static final Logger logger = LoggerFactory.getLogger(ApiController.class);


public void sync(final String mappingUri, final String rowKey, final String body) throws Exception {

SyncVO syncVo = new SyncVO();

syncVo.setMappingUri(mappingUri);

syncVo.setRowKey(rowKey);

syncVo.setBody(body);

syncService.insertPrsSync(syncVo);

}


private boolean isSyncProgram(HttpServletRequest request) {

return Common.isEquals(request.getHeader("User-Agent"), SyncJob.LARS_SYNC_PROGRAM);

}


/**

* 이용기관정보 연동 API

*

* @param session

* @param request

* @return

*/

@RequestMapping(value = "/ifs/Organization", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})

@ResponseBody

public ApiResponseVO Organization(@RequestBody String body, HttpServletRequest request) {

ApiResponseVO result = null;

OrganizationVO org = null;

String hstNo = "";

try {

result = chkParameter(body, request);

if (result != null) return result;


org = (OrganizationVO) JSONObject.toBean(Common.toJSONObject(body), OrganizationVO.class);

hstNo = Common.nvl(org.getHstNo());


result = makeInfoService.insertOrganization(org);


if (Common.isEquals(result.getResultCode(), ApiRspCode.OK) && !isSyncProgram(request)) {

sync("/ifs/Organization", hstNo, body);

}

} catch (Exception e) {

e.printStackTrace();

result = new ApiResponseVO(ApiRspCode.UNKNOWN_ERROR, hstNo);

}

return result;

}


/**

* 사용자정보 연동 API

*

* @param session

* @param request

* @return

*/

@RequestMapping(value = "/ifs/Customer", method = RequestMethod.POST)

@ResponseBody

public ApiResponseVO Customer(@RequestBody String body, HttpServletRequest request) {

ApiResponseVO result = null;

CustomerVO customer = null;

String hstNo = "";

try {

result = chkParameter(body, request);

if (result != null) return result;


customer = (CustomerVO) JSONObject.toBean(Common.toJSONObject(body), CustomerVO.class);

hstNo = Common.nvl(customer.getHstNo());


result = makeInfoService.insertCustomer(customer);


if (Common.isEquals(result.getResultCode(), ApiRspCode.OK) && !isSyncProgram(request)) {

sync("/ifs/Customer", hstNo, body);

}

} catch (Exception e) {

e.printStackTrace();

result = new ApiResponseVO(ApiRspCode.UNKNOWN_ERROR, hstNo);

}

return result;

}


/**

* 사용자정보 연동 API

*

* @param session

* @param request

* @return

*/

@RequestMapping(value = "/ifs/TalkGroup", method = RequestMethod.POST)

@ResponseBody

public ApiResponseVO talkGroup(@RequestBody String body, HttpServletRequest request) {

ApiResponseVO result = null;

TalkGroupVO talkGroup = new TalkGroupVO();

String hstNo = "";

try {

result = chkParameter(body, request);

if (result != null) return result;


JSONObject obj = Common.toJSONObject(body);

talkGroup.setHstNo(Common.nvl(obj.get("hstNo")));

talkGroup.setWrkDiv(Common.nvl(obj.get("wrkDiv")));

talkGroup.setDtType(Common.nvl(obj.get("dtType")));

talkGroup.setMcPttGroupId(Common.nvl(obj.get("MCPTTGroupID")));

talkGroup.setGroupDisplayName(Common.nvl(obj.get("GroupDisplayName")));

talkGroup.setUpperMCPTTGroupId(Common.nvl(obj.get("UpperMCPTTGroupID")));

talkGroup.setGroupDescription(Common.nvl(obj.get("GroupDescription")));

talkGroup.setGroupSortOrder(Common.nvl(obj.get("GroupSortOrder")));

talkGroup.setUseYn(Common.nvl(obj.get("useYn")));


//JsonConfig config = new JsonConfig();

//config.setSkipJavaIdentifierTransformationInMapKeys(false);


//talkGroup = (TalkGroupVO) JSONObject.toBean(Common.toJSONObject(body), TalkGroupVO.class, config);

//talkGroup = (TalkGroupVO) JSONObject.toBean(Common.toJSONObject(body), TalkGroupVO.class);

hstNo = Common.nvl(talkGroup.getHstNo());


result = makeInfoService.insertTalkGroup(talkGroup);


if (Common.isEquals(result.getResultCode(), ApiRspCode.OK) && !isSyncProgram(request)) {

sync("/ifs/TalkGroup", hstNo, body);

}

} catch (Exception e) {

e.printStackTrace();

result = new ApiResponseVO(ApiRspCode.UNKNOWN_ERROR, hstNo);

}

return result;

}


/**

* 단말정보 연동 API

*

* @param session

* @param request

* @return

*/

@RequestMapping(value = "/service/DeviceInfo", method = RequestMethod.POST)

@ResponseBody

public ApiResponseVO DeviceInfo(@RequestBody String body, HttpServletRequest request) {

ApiResponseVO result = null;

DeviceInfoVO deviceInfo = null;

String hstNo = "";

try {

result = chkParameter(body, request);

if (result != null) return result;


deviceInfo = (DeviceInfoVO) JSONObject.toBean(Common.toJSONObject(body), DeviceInfoVO.class);

hstNo = Common.nvl(deviceInfo.getHstNo());


result = makeInfoService.insertDeviceInfo(deviceInfo);


if (Common.isEquals(result.getResultCode(), ApiRspCode.OK) && !isSyncProgram(request)) {

sync("/service/DeviceInfo", hstNo, body);

}

} catch (Exception e) {

e.printStackTrace();

result = new ApiResponseVO(ApiRspCode.UNKNOWN_ERROR, hstNo);

}

return result;

}


private void printParameterLog(String body, HttpServletRequest request) {

logger.info("{} {} {}", request.getRemoteAddr(), request.getRequestURI(), body.replaceAll(System.getProperty("line.separator"), ""));

}


private ApiResponseVO chkParameter(String body, HttpServletRequest request) {

printParameterLog(body, request);

//Content-Type check

if (Common.nvl(request.getContentType()).indexOf("json") == -1) {

logger.error("Content-Type is not application/json.");

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.EMPTY, "Content-Type is not application/json.");

}


if (Common.isEmpty(body)) {

logger.error("Parameter is empty.");

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.EMPTY, "Parameter is Empty.");

}


JSON json = null;

try {

json = JSONSerializer.toJSON(body);

} catch (Exception e) {

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.EMPTY, "Parameter is not JSON type.");

}


if (!(json instanceof JSONObject)) {

logger.error("Parameter is not JSONObject type.");

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.EMPTY, "Parameter is not JSONObject type.");

} else {

JSONObject obj = (JSONObject) json;

if (Common.isEmpty(obj.get("hstNo"))) {

logger.error("hstNo is empty.");

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.EMPTY, "hstNo is empty.");

}

if (Common.isEmpty(obj.get("wrkDiv"))) {

logger.error("wrkDiv is empty.");

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.nvl(obj.get("hstNo")), "wrkDiv is empty.");

}

if (!Common.isOrEquals(obj.get("wrkDiv"), "I", "U", "D")) {

logger.error("wrkDiv is invalid data.");

return new ApiResponseVO(ApiRspCode.PARAMETER_ERROR, Common.nvl(obj.get("hstNo")), "wrkDiv is invalid data.");

}

}


return null;

}

}

반응형