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
'JAVA > Java' 카테고리의 다른 글
java list에서 key값 뽑기, list를 map으로 만들기 (0) | 2022.05.24 |
---|---|
java, mybatis resulttype=hashmap, map형태로 데이터 받기 (0) | 2022.05.19 |
Restful API 개발 (2) 정보 조회 (0) | 2022.03.25 |
Restful API 개발 (1) 데이터 저장 api개발 및 api테스트 사이트 (0) | 2022.03.25 |
Map에 넣은 순서대로 나오게 하는 방법 (HashMap, LinkedHashMap) (0) | 2022.03.25 |
댓글