尝试设置一个更新数据库表的宁静服务组件。尝试同时使用 Spring RestTemplate 和 apache commons restful impl,但似乎都没有工作。
关于使用
选项 1:使用 Spring RestTemplate:导致以下错误
com.fasterxml.jackson.databind.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 java.util.LinkedHashMap 的实例
选项 2:使用 org.apache.commons.httpclient。方法.PostMethod; 导致以下错误
服务器端错误:
org.codehaus.jackson.JsonParseException:意外字符('<'(代码 60)):预期有效值(数字、字符串、数组、对象、'true'、'false' 或 '空值')
客户端错误:
服务器拒绝此请求,因为请求实体的格式不受所请求方法 () 的请求资源支持。
我的 Restful 服务方法被注释为“Post”并使用“JSON”。我的客户端控制器启动 RestFul 调用,代码如下
@RequestMapping(value="/update", consumes="application/json")
public void updateMaintReport(
@RequestBody Map<String, String> formModelData,
HttpServletRequest request,
HttpServletResponse response)
throws IOException,JsonMappingException {
logger.log(LogLevel.DEBUG, "REACHED method updateMaintReport..");
System.out.println("Reached method updateMaintReport.....");
boolean errorEncountered = false;
ReportingSession repSession = null;
HttpSession session = request.getSession(false);
if(session==null) {
// TODO: code for handling invalid/expired session
} else {
repSession = (ReportingSession)session.getAttribute(ReportingWebConstants.REPORTING_SESSION);
if(repSession==null) {
errorEncountered = true;
}
}
if(!errorEncountered) {
ServiceClient serviceClient = new ServiceClient();
String servicesUrl = this.environment.getProperty("service_url_reports_data");
String servicesName = this.environment.getProperty("service_name_reports_update_fnol");
String serviceUrl = VIPUrlFactory.getServiceUrl(servicesUrl+servicesName);
logger.log(LogLevel.DEBUG, "For update : serviceUrl: "+serviceUrl);
//Option 1: Using Spring RestTemplate :
LinkedMultiValueMap<String,String> headers = new LinkedMultiValueMap<String,String>();
headers.add("Accept","application/json");
headers.add("Content-type","application/json");
List list = new ArrayList<Map<String, String>>(); list.add(formModelData);
RestTemplate restTemplate = new RestTemplate();
HttpEntity<List> requestEntity = new HttpEntity<List>(list, headers);
ResponseEntity<List> fList = restTemplate.exchange(serviceUrl,
HttpMethod.POST,
requestEntity,
List.class);
//Option 2: using org.apache.commons.httpclient.methods.PostMethod; -- Will be commented when option 1 block is uncommented
serviceClient.setParams(formModelData);
serviceClient.setServiceUrl(serviceUrl);
serviceClient.callRestServicePost();
logger.log(LogLevel.DEBUG, "Posting data to service - to execute the update");
}
}
在上面的代码中,选项 1 和选项 2 块不会同时执行。
下面是接受 Restful 调用的代码块,我的服务器端代码。
@RequestMapping(value = "/update", method = RequestMethod.POST)
public void updateMainRptData(@RequestBody Map<String, String> formModelData) throws ReportingIntegrationException,
IOException, JsonMappingException {
String updateStmt = "UPDATE CL_SCRIPTS SET DELETE_IND = #{delete_ind}, SCRIPT_DESC = #{script_desc}, SCRIPT_TXT = #{script_txt}WHERE COMPANY_CD = #{company_cd} AND SCRIPT_NAME = #{script_name}AND PROMPT_ID = #{prompt_id}";
ParameterObjectDTO paramObjDTO = new ParameterObjectDTO();
logger.log(LogLevel.DEBUG,"In Services Web: updateMainRptData()");
if(!formModelData.isEmpty()) {
Set<String> keySet = formModelData.keySet();
StringBuilder sb = new StringBuilder();
for (String key : keySet) {
sb.append(key).append(" -- ").append(formModelData.get(key)).append("\n");
}
logger.log(LogLevel.DEBUG, sb.toString());
}
paramObjDTO.setModalForQuery(formModelData);
paramObjDTO.setUpdateSqlStmt(updateStmt);
maintReportingSvc.updateMaintReport(paramObjDTO);
}
我在浏览器中看到的错误消息没有帮助,但我相信我的 JSON 数据是有效的。任何帮助表示赞赏。谢谢。