在将其上传到alfresco时,如何将自定义属性添加到文件中

发布于 2025-02-03 02:51:20 字数 5768 浏览 3 评论 0原文

我使用 alfresco的核心api 上传文件,上传文件我的版本我的Alfresco是社区-6.1.2 我有一个春季启动项目,可以在alfresco中上传文件,我想添加一些自定义属性 这是我的代码

    URI uri = new URI(endpoint + nodeId + "/children");

    LinkedMultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    String pathfile = "C:\\tmp\\alfrescorepo\\"+filename;
    FileUtils.writeByteArrayToFile(new File(pathfile), bytefile);
    File file = new File(pathfile);
    params.add("filedata", new FileSystemResource(file));
    params.add("majorVersion", true);
    params.add("versioningEnabled", true);
    
    JsonObject customProperties = new JsonObject();
    customProperties.addProperty("title", "sample");
    customProperties.addProperty("dataType", "cm:text");
    customProperties.addProperty("description", "example");
    customProperties.addProperty("defaultValue", "10");
    
    params.add("properties", customProperties);
    
    HttpEntity<Map<String, String>> request = new HttpEntity(params,headers);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
    ResponseEntity<NodeEntry> response = restTemplate.postForEntity(uri, request, NodeEntry.class);
    
    return response.getBody().getEntry();
        

,我有这个错误

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: JsonObject; nested exception is com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asBoolean"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:465)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.writeInternal(AbstractGenericHttpMessageConverter.java:113)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:227)
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:541)
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:517)
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:497)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:369)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:156)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:991)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:774)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:486)
    at com.xpfibre.alfresco.repository.file.FileAlfrescoRepository.create(FileAlfrescoRepository.java:105)
    at com.xpfibre.alfresco.repository.AlfrescoRepositoryApplication.main(AlfrescoRepositoryApplication.java:41)
    ... 5 more
Caused by: com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asBoolean"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:392)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:351)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:782)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1518)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1007)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456)
    ... 18 more
Caused by: java.lang.UnsupportedOperationException: JsonObject
    at com.google.gson.JsonElement.getAsBoolean(JsonElement.java:153)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:689)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
    ... 24 more

I use the CORE API of Alfresco to upload a file my version of alfresco is Community - 6.1.2
I have a spring boot project that upload a file in alfresco and I want to add some custom properties
Here is my code

    URI uri = new URI(endpoint + nodeId + "/children");

    LinkedMultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
    String pathfile = "C:\\tmp\\alfrescorepo\\"+filename;
    FileUtils.writeByteArrayToFile(new File(pathfile), bytefile);
    File file = new File(pathfile);
    params.add("filedata", new FileSystemResource(file));
    params.add("majorVersion", true);
    params.add("versioningEnabled", true);
    
    JsonObject customProperties = new JsonObject();
    customProperties.addProperty("title", "sample");
    customProperties.addProperty("dataType", "cm:text");
    customProperties.addProperty("description", "example");
    customProperties.addProperty("defaultValue", "10");
    
    params.add("properties", customProperties);
    
    HttpEntity<Map<String, String>> request = new HttpEntity(params,headers);
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
    ResponseEntity<NodeEntry> response = restTemplate.postForEntity(uri, request, NodeEntry.class);
    
    return response.getBody().getEntry();
        

And I have this error

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: JsonObject; nested exception is com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asBoolean"])
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:465)
    at org.springframework.http.converter.AbstractGenericHttpMessageConverter.writeInternal(AbstractGenericHttpMessageConverter.java:113)
    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:227)
    at org.springframework.http.converter.FormHttpMessageConverter.writePart(FormHttpMessageConverter.java:541)
    at org.springframework.http.converter.FormHttpMessageConverter.writeParts(FormHttpMessageConverter.java:517)
    at org.springframework.http.converter.FormHttpMessageConverter.writeMultipart(FormHttpMessageConverter.java:497)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:369)
    at org.springframework.http.converter.FormHttpMessageConverter.write(FormHttpMessageConverter.java:156)
    at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:991)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:774)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751)
    at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:486)
    at com.xpfibre.alfresco.repository.file.FileAlfrescoRepository.create(FileAlfrescoRepository.java:105)
    at com.xpfibre.alfresco.repository.AlfrescoRepositoryApplication.main(AlfrescoRepositoryApplication.java:41)
    ... 5 more
Caused by: com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asBoolean"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:392)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:351)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:782)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1518)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:1007)
    at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:456)
    ... 18 more
Caused by: java.lang.UnsupportedOperationException: JsonObject
    at com.google.gson.JsonElement.getAsBoolean(JsonElement.java:153)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:689)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:774)
    ... 24 more

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2025-02-10 02:51:20

您的错误是关于布尔参数的:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asBoolean"])

params majorversionversioningEnabled需要在URL中添加为参数:

URI uri = new URI(endpoint + nodeId + "/children?majorVersion=true&versioningEnabled=true");

加上,如果您想使用alfresco的基本属性,例如desctiption和标题,您的JSON应该看起来像这样:

{
  "name":"filename",
  "nodeType":"cm:content", //this is default type for file
  "properties":
  {
    "cm:title":"File title",
    "cm:description":"This is an description, default metatda"
  }
}

如果要添加自定义类型和自定义属性,则需要将自定义内容模型添加到Alfresco,请参阅:
<
关于自定义内容模型的好教程

Your error is about boolean parameters:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asBoolean"])

The params majorVersion and versioningEnabled need to be added as parameters in the Url :

URI uri = new URI(endpoint + nodeId + "/children?majorVersion=true&versioningEnabled=true");

Plus, if you want to use basic properties of alfresco like desctiption and title, your Json should look like this :

{
  "name":"filename",
  "nodeType":"cm:content", //this is default type for file
  "properties":
  {
    "cm:title":"File title",
    "cm:description":"This is an description, default metatda"
  }
}

If you want to add custom types and custom properties, you need to add custom content model to Alfresco, see:
Alfresco Docs : Content models |
Good Tutorial About custom content models

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文