refactor: restTemplate 모듈화
This commit is contained in:
parent
deef4ca1ad
commit
3b0001618f
44
src/main/java/com/itn/mjonApi/cmn/apiServer/ApiService.java
Normal file
44
src/main/java/com/itn/mjonApi/cmn/apiServer/ApiService.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.itn.mjonApi.cmn.apiServer;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.itn.mjonApi.cmn.msg.MjonResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
* packageName : com.itn.mjonApi.cmn.apiServer
|
||||
* fileName : ApiService
|
||||
* author : hylee
|
||||
* date : 2023-05-15
|
||||
* description :
|
||||
* ===========================================================
|
||||
* DATE AUTHOR NOTE
|
||||
* -----------------------------------------------------------
|
||||
* 2023-05-15 hylee 최초 생성
|
||||
*/
|
||||
@Service
|
||||
public class ApiService <T> {
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
public ApiService(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
public MjonResponse postForEntity(String url, Object request, Class<String> responseType) throws JsonProcessingException {
|
||||
ResponseEntity<String> spamChkEntity = (ResponseEntity<String>) restTemplate.postForEntity(
|
||||
url
|
||||
, request
|
||||
, responseType
|
||||
);
|
||||
|
||||
MjonResponse spamResponse = MjonResponse.getMjonResponse(spamChkEntity);
|
||||
|
||||
|
||||
return spamResponse;
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,10 @@
|
||||
package com.itn.mjonApi.cmn.msg;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
/**
|
||||
* packageName : com.itn.mjonApi.cmn.msg
|
||||
@ -24,4 +27,20 @@ public class MjonResponse {
|
||||
private String resultBlockSts; // 수신거부 갯수
|
||||
private String afterCash;
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stringResponseEntity
|
||||
* @return ResponseEntity vo convert
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
public static MjonResponse getMjonResponse(ResponseEntity<String> stringResponseEntity) throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
MjonResponse mjonResponse = objectMapper.readValue(stringResponseEntity.getBody(), MjonResponse.class);
|
||||
return mjonResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,48 +1,47 @@
|
||||
package com.itn.mjonApi.mjon.api.send.service.impl;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.itn.mjonApi.cmn.apiServer.ApiService;
|
||||
import com.itn.mjonApi.cmn.msg.MjonResponse;
|
||||
import com.itn.mjonApi.mjon.api.send.mapper.domain.MjonMsgVO;
|
||||
import com.itn.mjonApi.mjon.api.send.service.SendService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.apache.catalina.connector.Response;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SendServiceImpl implements SendService {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private ApiService<Response> apiService;
|
||||
|
||||
public SendServiceImpl(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
@Autowired
|
||||
public SendServiceImpl(ApiService<Response> apiService) {
|
||||
this.apiService = apiService;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MjonResponse sendMsgData(MjonMsgVO mjonMsgVO) throws Exception {
|
||||
|
||||
ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(
|
||||
// 스팸체크 하는 부분
|
||||
MjonResponse spamChkEntity = apiService.postForEntity(
|
||||
"/web/user/login/selectSpamTxtChkAjax.do"
|
||||
, mjonMsgVO
|
||||
, String.class
|
||||
);
|
||||
|
||||
// 문자 전송하는 부분
|
||||
MjonResponse munjaSendResponse = apiService.postForEntity(
|
||||
"/web/user/login/sendMsgDataAjax.do"
|
||||
, mjonMsgVO
|
||||
, String.class
|
||||
);
|
||||
MjonResponse mjonResponse = getMjonResponse(stringResponseEntity);
|
||||
|
||||
return mjonResponse;
|
||||
|
||||
|
||||
return spamChkEntity;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param stringResponseEntity
|
||||
* @return ResponseEntity vo convert
|
||||
* @throws JsonProcessingException
|
||||
*/
|
||||
private static MjonResponse getMjonResponse(ResponseEntity<String> stringResponseEntity) throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
MjonResponse mjonResponse = objectMapper.readValue(stringResponseEntity.getBody(), MjonResponse.class);
|
||||
return mjonResponse;
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,6 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@ -51,7 +50,6 @@ public class SendRestController {
|
||||
@PostMapping("/api/sendTest")
|
||||
public MjonResponse sendTest(MjonMsgVO mjonMsgVO) throws Exception {
|
||||
|
||||
|
||||
return sendService.sendMsgData(mjonMsgVO);
|
||||
|
||||
}
|
||||
@ -83,57 +81,10 @@ public class SendRestController {
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return mjonResponse;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mjonMsgVO
|
||||
* @Discription 리턴 데이터 테스트
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/api/returnTest")
|
||||
public Object returnTest(MjonMsgVO mjonMsgVO) throws IOException {
|
||||
|
||||
System.out.println("test !!!");
|
||||
|
||||
|
||||
|
||||
|
||||
// API에 요청할 URL
|
||||
String apiUrl = "http://localhost:8080/web/user/login/returnTest.do";
|
||||
|
||||
// RestTemplate 객체 생성
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
// API로부터 ResponseEntity<ModelAndView> 응답 받기
|
||||
ResponseEntity<ModelAndView> response = restTemplate.getForEntity(apiUrl, ModelAndView.class);
|
||||
|
||||
// ModelAndView 추출 및 데이터 출력
|
||||
ModelAndView modelAndView = response.getBody();
|
||||
System.out.println("View Name: " + modelAndView.getViewName());
|
||||
System.out.println("Model: " + modelAndView.getModel());
|
||||
|
||||
|
||||
// ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity(
|
||||
// "/web/user/login/returnTest.do"
|
||||
// , mjonMsgVO
|
||||
// , Object.class
|
||||
// );
|
||||
|
||||
// System.out.println("objectResponseEntity :: " + objectResponseEntity);
|
||||
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -14,4 +14,4 @@ server.port=8088
|
||||
|
||||
logging.level.root=info
|
||||
|
||||
api.root.url=http://localhost:8080
|
||||
api.root.url=http://192.168.0.125:8095/
|
||||
@ -10,9 +10,8 @@ spring.datasource.url=jdbc:log4jdbc:mysql://192.168.0.125:3306/mjon?serverTimezo
|
||||
spring.datasource.username=mjonUr
|
||||
spring.datasource.password=mjon!@#$
|
||||
|
||||
|
||||
server.port=9100
|
||||
|
||||
logging.level.root=info
|
||||
|
||||
api.root.url=http://localhost:8080
|
||||
api.root.url=http://192.168.0.125:8095/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user