尝试设置更新数据库表的restful服务组件。尝试使用Spring RestTemplate以及apache commons restful impl,两者似乎都没有用。
使用
选项1:使用Spring RestTemplate:导致以下错误的结果
com.fasterxml.jackson.databind.JsonMappingException:无法从START_ARRAY标记中反序列化java.util.LinkedHashMap的实例
选项2:使用org.apache.commons.httpclient.methods.PostMethod;导致以下错误
服务器端错误:
org.codehaus.jackson.JsonParseException:意外字符('&lt;'(代码60)):预期有效值(数字,字符串,数组,对象,'true','false'或'null')< / strong>
客户端错误:
服务器拒绝了此请求,因为请求实体所采用的方法所请求的资源不支持该格式()。
我的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 call的代码块,我的服务器端代码。
@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数据是有效的。任何帮助表示赞赏。谢谢。
答案 0 :(得分:0)
后来我更改了方法updateMainRptData的签名,并添加了返回类型和@ResponseBody来解决此问题。