Cute Happy Ghost
본문 바로가기
JAVA/Java

Restful API 개발 (3) dataType별(json,xml) 출력하기, controller에서 xml json변환하기

by JENN_tech7 2022. 3. 28.
728x90
SMALL

먼저 dependency추가하고 기본 format을 xml로 해놓기

<dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-xml</artifactId>
      <version>${jackson.version}</version>
</dependency>

 

 

 

 

 

 

1. 분기점이 되는 mtd

- 기본 : json type

만약 parameter의 dataType이 xml이라면 contentType을 xml로 변경해주기

- location 을 반환

@RequestMapping(value="/api/{name}/{id}/list")
    public String selectList(HttpServletRequest request, RedirectAttributes redirectAttributes
    		,@PathVariable("name") String name,@PathVariable("id") String id) throws Exception {

    	Map paramMap = getParameterMap(request, true);
    	String contentType = "json";

		if(!CommUtils.isEmpty((String)paramMap.get("dataType")) && ((String) paramMap.get("dataType")).equalsIgnoreCase("xml")) {
			contentType = "xml";
		}
	    String location = "forward:/api/" + name + "/" + id + "/" + "list" + contentType;
	    
        return location;
    	
    }

 

 

 

 

2. 데이터 출력 형식을 입혀주는 mtd

json, xml로 들어온 애들에게 produces 사용해서 각각 json, xml을 입혀주고 RunSelectList함수 호출

필요한 값들만 넣어서 넘겨준다

    @RequestMapping(value="/api/{name}/{id}/listjson", produces = {ApiConst.APPLICATION_JSON_VALUE})
    @ResponseBody
    public Map selectListJson(HttpServletRequest request,@PathVariable("name") String name,@PathVariable("id") String id) throws Exception {
    	
    	return RunselectList(name,id);
    	
    }
    

    @RequestMapping(value="/api/{name}/{id}/listxml", produces = {ApiConst.APPLICATION_XML_VALUE})
    @ResponseBody
    public Map selectListXml(HttpServletRequest request,@PathVariable("name") String name,@PathVariable("id") String id) throws Exception {

    	return RunselectList(name,id);
    	
    }

 

 

 

 

3. 실제 데이터를 불러오는 mtd

public Map RunselectList(@PathVariable("name") String name,@PathVariable("id") String id) throws Exception {
    	    	
                HashMap finalresultMap = new LinkedHashMap();
    	        HashMap header = new LinkedHashMap(); //상태코드
    	        HashMap resultMap = new LinkedHashMap(); //데이터

    	        String resultCode = "00";
    	        String tn = ApiConst.ApiNameMap.get(name) + id;
    	        try{
    	        	header.put("returnAuthMsg", ApiConst.ApiResultMap.get(resultCode));
    	        	header.put("returnReasonCode", resultCode);

    	        	List list = apiService.selectList(tn);
    	        	
    	        	resultMap.put("header",header);
    	        	resultMap.put("data",list);
    	        	finalresultMap.put(name+id, resultMap);
    	        }
    	        
    	        catch (Exception e) {
    	    		resultCode = "99";
    	    		finalresultMap.put("errMsg", ApiConst.ERR_MSG);
    	    		finalresultMap.put("returnAuthMsg", ApiConst.ApiResultMap.get(resultCode));
    	    		finalresultMap.put("returnReasonCode", resultCode);
    	    	}
    	        return finalresultMap;
    	    }

 

 

 

 

결과

  • dataType = json일 때 (and 기본)

  • dataType = xml일 때

 

728x90
LIST

댓글