egovframework(Spring Framework)에서 *.do와 RESTFul API 동시 사용 설정 방법입니다.

인터넷에 찾아보면, web.xml 설정하는 방법만 나와있어서...

전자정부 프레임워크(3.8)에서 테스트하였습니다.

(1) 수정 대상 파일

package egovframework.com.cmm.config;

/**
 * EgovWebApplicationInitializer 클래스
 */

public class EgovWebApplicationInitializer implements WebApplicationInitializer {

	private static final Logger LOGGER = LoggerFactory.getLogger(EgovWebApplicationInitializer.class);
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		LOGGER.debug("EgovWebApplicationInitializer START-============================================");
		
        .....
        
		FilterRegistration.Dynamic characterEncoding = servletContext.addFilter("encodingFilter", new org.springframework.web.filter.CharacterEncodingFilter());
		characterEncoding.setInitParameter("encoding", "UTF-8");
		characterEncoding.setInitParameter("forceEncoding", "true");
		characterEncoding.addMappingForUrlPatterns(null, false, "*.do");
		//characterEncoding.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "*.do");
		//-------------------------------------------------------------
		// RESTFul API 사용을 위해서 UrlPatterns 추가 (2021.04.15. 황상규)
		//-------------------------------------------------------------
		characterEncoding.addMappingForUrlPatterns(null, false, "/*");
        
        ....
}

(3) RESTFul API URL Controller (샘플)

package egovframework.smms.statistics.report;


import java.util.Enumeration;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import egovframework.ecs.common.util.JsonUtil;
import egovframework.rte.psl.dataaccess.util.EgovMap;
import egovframework.smms.management.resource.ResourceService;
import net.minidev.json.JSONObject;

/**
* RESTFul API 샘플
* @author Hwang Sanggyu
* @since 2021.04.21
* @version 1.0
* @see
*/
@RestController
public class RestFulController {
	
	//@Autowired
	//private ResourceService defaultService;
	
	//*********************************************************************************************************
	@RequestMapping(value="/cards/{cardNo}/approval-overseas", method=RequestMethod.GET)
	public ModelAndView cards(@PathVariable("cardNo") String cardNo, HttpServletRequest request) throws Exception {	
		    	
    	Enumeration<String> eHeader = request.getHeaderNames();    	
    	HashMap<String, Object> headerMap = new HashMap<String, Object>();    	
    	while(eHeader.hasMoreElements()) {
    		String headerName = eHeader.nextElement();
    		headerMap.put(headerName, request.getHeader(headerName));
    	}
    	    	
    	HashMap<String, Object> tempMap = new HashMap<String, Object>();
    	
    	if("application/json".equals(headerMap.get("content-type"))){
    		//JSONObject jsonObject = JsonUtil.getStringToJsonObecjt(request); 	
    	}
    		    	
    	StringBuffer requestURL = request.getRequestURL();
    	String servletPath = request.getServletPath();
    	
    	ModelAndView mav 	= new ModelAndView("jsonView");
		  EgovMap resultMap 	= new EgovMap();

		try {
			// 목록 조회
			//mav.addObject("dataList"		, defaultService.select(tempMap)); // DB SQL 실행 테스트
			
	    resultMap.put("result"			, "SUCCESS");
	    resultMap.put("message"			, "정상적으로 조회되었습니다.");
		} catch(Exception ex) {
			resultMap.put("result"			, "ERROR");
	    resultMap.put("message"			, "조회중 오류가 발생하였습니다");	    
			ex.printStackTrace();
		} finally {
			mav.addObject("result"			, resultMap);		
		}  

		return mav;
    }	
}
Posted by 우라질레이터

urajilation@gmail.com
우라질레이터

달력

태그목록