import com.fasterxml.jackson.databind.ObjectMapper; …… public class SearchAction extends BaseAction { …… /** * Open Api: 特定資源檢索開放接口 * @param request * @param response */ public void find_api(HttpServletRequest request, HttpServletResponse response) { ObjectMapper objectMapper = new ObjectMapper(); String jsonString = ""; Map<String, Object> map = new HashMap<String, Object>(); boolean success = false; StringBuffer error = new StringBuffer(); try{ // 必須參數:type key String type = request.getParameter("type"); String key = request.getParameter("key"); if (StringUtils.isNotBlank(type) && StringUtils.isNotBlank(key) && key.trim().length() >= 3){ // 根據type到OpenApi/Search分類下獲取對應配置 SearchOpenApi apiBean = this.getSearchService().getApiBeanByType(type.trim()); // 獲取檢索結果 List<SearchBean> dataList = this.getSearchService().findSearchBeansByKeyAndApi(key.trim(), apiBean); if (null != dataList && dataList.size() > 0) { //生成指定反饋json success = true; map.putAll(this.getSearchBeanListJson(dataList)); } else { success = false; error.append("沒有符合條件的記錄!"); } } else { success = false; error.append("參數錯誤!"); } }catch(Exception e){ LogUtility.logError("find_api error", e); success = false; error.append(e.toString()); } map.put("success", success); if (!success && error.length() > 0) map.put("error", error.toString()); try { jsonString = objectMapper.writeValueAsString(map); } catch (JsonProcessingException e) { LogUtility.logError("find_api JsonProcessingException", e); } outPut(request,response, jsonString); } }