본문 바로가기
기타/프로그래밍 관련

전자정부프레임워크 SELECT JSON 처리

by WebHack 2014. 2. 19.

Json lib Maven(pom.xml)

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-mapper-asl</artifactId>

<version>1.9.11</version>

<type>jar</type>

<scope>compile</scope>

</dependency>

<dependency>

<groupId>org.codehaus.jackson</groupId>

<artifactId>jackson-core-asl</artifactId>

<version>1.9.11</version>

<type>jar</type>

<scope>compile</scope>

</dependency>



Controller

package egovframework.lhj.web;


import java.io.PrintWriter;

import java.util.*;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import egovframework.lhj.service.WebDBService;

import egovframework.lhj.service.WebDBVO;


@Controller

public class WebDBController {

@Resource(name = "WebDBService")

private WebDBService webDBService;

@RequestMapping("/lhj/getWebDB.do")

@ResponseBody

public void getWebDB(Model model , HttpServletRequest request ,HttpServletResponse response ) throws Exception {

WebDBVO webDBVO = new WebDBVO();

String str_resData = "";

String str_seq =  request.getParameter("seq");

webDBVO.setSeq(str_seq);

webDBVO.setAdapter_type("004");

List listGetWebData = webDBService.selectWebDBList(webDBVO);

    String str_json_data_name = "GetWebData";

try

{

Map<String, Object> map_d = new HashMap<String, Object>();

if(!listGetWebData.isEmpty())

{

map_d.put(str_json_data_name, listGetWebData);

}

else

{

map_d.put(str_json_data_name, "No Data");

}

    ObjectMapper om = new ObjectMapper();

    str_resData = om.writeValueAsString(map_d);

}

catch(Exception ex)

{

System.out.println(this.getClass().toString() + " Exception : " + ex.toString());

Map<String, Object> map_d = new HashMap<String, Object>();

    map_d.put(str_json_data_name, "Error : " + ex.toString());

    ObjectMapper om = new ObjectMapper();

    str_resData = om.writeValueAsString(map_d);

}

response.setContentType("text/html; charset=utf-8");

    PrintWriter pw = response.getWriter();

    pw.print(str_resData);

    pw.flush();     

}

}


Service

package egovframework.lhj.service;


import java.util.List;


public interface WebDBService {


public List selectWebDBList(WebDBVO webDBVO) throws Exception;

}


ServiceImpl

package egovframework.lhj.service.impl;


import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import egovframework.lhj.service.WebDBService;

import egovframework.lhj.service.WebDBVO;

import egovframework.rte.fdl.cmmn.AbstractServiceImpl;

import egovframework.rte.fdl.idgnr.EgovIdGnrService;


@Service("WebDBService")

public class WebDBServiceImpl extends AbstractServiceImpl implements WebDBService {


    @Resource(name="WebDBDAO")

    private WebDBDAO webDBDAO;

    

    public List selectWebDBList(WebDBVO webDBVO) throws Exception {

    return webDBDAO.selectWebDBList(webDBVO);

    }

}


DAO

package egovframework.lhj.service.impl;


import java.util.List;

import org.springframework.stereotype.Repository;

import egovframework.lhj.service.WebDBVO;

import egovframework.rte.psl.dataaccess.EgovAbstractDAO;


@Repository("WebDBDAO")

public class WebDBDAO  extends EgovAbstractDAO {

public List selectWebDBList(WebDBVO webDBVO) throws Exception {

return list("WebDBSql.selectWebDBList", webDBVO);

}

}


SQL

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">


<sqlMap namespace="WebDBSql">

<typeAlias  alias="egovMap" type="egovframework.rte.psl.dataaccess.util.EgovMap"/>

<typeAlias  alias="WebDBVO" type="egovframework.lhj.service.WebDBVO"/>

<select id="WebDBSql.selectWebDBList" parameterClass="WebDBVO" resultClass="WebDBVO">

SELECT ADAPTER_NAME, ADAPTER_M_URL, ADAPTER_URL, ADAPTER_METHOD, CHARSET, REFERER 

FROM TB_ADAPTERINFO 

WHERE ADAPTER_TYPE = #adapter_type#

<isNotEmpty property="seq">

AND SEQ = #seq#

</isNotEmpty>

</select>

</sqlMap>


VO

package egovframework.lhj.service;


import java.io.Serializable;


@SuppressWarnings("serial")

public class WebDBVO implements Serializable{

private String seq = "";

private String adapter_type = "";

private String adapter_name = "";

private String adapter_m_url = "";

private String adapter_url = "";

private String adapter_method = "";

private String charset = "";

private String referer = "";

public String getSeq() {

return seq;

}


public void setSeq(String seq) {

this.seq = seq;

}


public String getAdapter_type() {

return adapter_type;

}


public void setAdapter_type(String adapter_type) {

this.adapter_type = adapter_type;

}


public String getAdapter_name() {

return adapter_name;

}


public void setAdapter_name(String adapter_name) {

this.adapter_name = adapter_name;

}


public String getAdapter_m_url() {

return adapter_m_url;

}


public void setAdapter_m_url(String adapter_m_url) {

this.adapter_m_url = adapter_m_url;

}


public String getAdapter_url() {

return adapter_url;

}


public void setAdapter_url(String adapter_url) {

this.adapter_url = adapter_url;

}


public String getAdapter_method() {

return adapter_method;

}


public void setAdapter_method(String adapter_method) {

this.adapter_method = adapter_method;

}


public String getCharset() {

return charset;

}


public void setCharset(String charset) {

this.charset = charset;

}


public String getReferer() {

return referer;

}


public void setReferer(String referer) {

this.referer = referer;

}

}



요청 URL  : http://127.0.0.1:8080/lhj/getWebDB.do?seq=203

결과물 :

{"GetWebData":[{"seq":"","adapter_type":"","adapter_name":"Daum","adapter_m_url":"/grd/getWebSource.do","adapter_url":"http://media.daum.net/digital/","adapter_method":"GET","charset":"UTF-8","referer":null}]}