使用Java 8更新Dataverse查找列遇问题求助
解决Dataverse查找列更新的400 Bad Request问题
错误核心分析
从报错信息来看,问题集中在两个关键点:
- 使用
PUT请求进行字段更新,Dataverse要求部分字段更新必须用PATCH方法 - 查找列的属性名称或绑定格式不正确,导致OData无法识别该导航属性
解决方案
1. 替换请求方法为PATCH
Dataverse的OData API中,PUT用于全量替换实体(需传递所有必填字段),仅更新单个字段应使用PATCH方法,否则会触发不必要的实体验证或解析错误。
2. 确认查找列的逻辑名称
报错提示undeclared property 'uniqa_mailtype',说明你使用的字段名称不是目标实体的有效属性。Dataverse中查找列的逻辑名称通常以id结尾,比如关联表为uniqa_mailtype时,查找列的逻辑名称应为uniqa_mailtypeid,而非uniqa_mailtype。
3. 验证@odata.bind的格式
绑定路径必须使用关联表的复数逻辑名称+记录GUID,格式为:
"/[关联表复数逻辑名称]([GUID])"
例如关联表逻辑名称是uniqa_mailtype,则实体集为uniqa_mailtypes,绑定路径应为"/uniqa_mailtypes(GUID_of_row)"。
4. 确认目标实体集名称
更新的表nameTableForCorrect必须是Dataverse中该表的复数逻辑名称(如accounts对应account表),不能使用显示名称或自定义别名。
修改后的代码示例
public static void main(String[] args) throws Exception { // 注意:value直接传递正确的绑定路径,columnForEditting使用查找列的逻辑名称(如uniqa_mailtypeid) DataverseController.updateColumnEMailMVPLookup( "/uniqa_mailtypes(GUID_of_row)", "GUID_row_id_in_table_for_change", token, "https://myOrganiztion.crm4.dynamics.com/api/data/v9.2/", "uniqa_mailtypeid" ); } public static void updateColumnEMailMVPLookup( String value, String row_id, String token, String url, String columnForEditting ) throws IOException { Map<String, Object> mapUpdate = new HashMap<String, Object>(); if ((value != null) && (value.trim().length() > 0)) { mapUpdate.put(columnForEditting + "@odata.bind", value); } JSONObject obj = new JSONObject(mapUpdate); String jsonString = obj.toString(); // 确保nameTableForCorrect是目标表的复数逻辑名称 String uri = url + "nameTableForCorrect(" + row_id + ")"; URL urlFinal = new URL(uri); HttpURLConnection httpURLConnection = (HttpURLConnection) urlFinal.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("PATCH"); // 替换为PATCH方法 httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.setRequestProperty("Accept", "application/json"); try (OutputStream outputStream = httpURLConnection.getOutputStream()) { outputStream.write(jsonString.getBytes()); outputStream.flush(); } int responseCode = httpURLConnection.getResponseCode(); StringBuilder response = new StringBuilder(); if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) { System.out.println("Update successful."); } else { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream()))) { String line; while ((line = bufferedReader.readLine()) != null) { response.append(line); } } System.err.println("Update failed: " + responseCode + " " + httpURLConnection.getResponseMessage()); System.err.println("Error response: " + response.toString()); } }
内容的提问来源于stack exchange,提问作者Vlad
相关产品推荐
相关产品推荐

