how to add custom properties to a file when uploading it in alfresco

428 Views Asked by At

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
1

There are 1 best solutions below

0
On

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