Alfresco 4.2.c rest api: create site giving error

2019-08-03 15:32发布

I am trying to create a site using alfresco 4.2.c rest api /alfresco/wcservice/api/sites (PUT) I am using admin user so no issues with permissions. I am able to create site successfully using the alfresco share UI

String objectToPost = "{\"shortName\":\"firm007\", \"title\":\"firm007\", \"description\":\"firm007\", \"visibility\":\"PRIVATE\", \"isPublic\":\"false\", \"sitePreset\":\"site-dashboard\"}";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<String> entity = new HttpEntity<String>(objectToPost,headers);

        String url = PRE_URL + "/alfresco/wcservice/api/sites"+"?alf_ticket="+ TICKET;
        String response = restTemplate.exchange(url,HttpMethod.POST, entity, String.class, new Object[] {}).toString();
        System.out.println("Created SITE response is =" + response);    

I am getting 500 Internal Server Error on client side code and on alfresco server, I am getting following stack traces.

please can you help solve this issue. I want to create an alfresco site, through a remote REST api call

2014-06-17 08:39:38,232 ERROR [freemarker.runtime] [http-apr-8080-exec-4] Template processing error: "Expression site.shortName is undefined on line 9, column 56 in org/alfresco/repository/site/site.lib.ftl."

Expression site.shortName is undefined on line 9, column 56 in org/alfresco/repository/site/site.lib.ftl.

The problematic instruction:

==> ${url.serviceContext + "/api/sites/" + site.shortName} escaped ${jsonUtils.encodeJSONString(url.serviceContext + "/api/sites/" + site.shortName)} [on line 9, column 17 in org/alfresco/repository/site/site.lib.ftl] in user-directive siteJSONManagers [on line 2, column 1 in org/alfresco/repository/site/site.lib.ftl]

in user-directive siteLib.siteJSON [on line 2, column 1 in org/alfresco/repository/site/sites.post.json.ftl]

Java backtrace for programmers:

freemarker.core.InvalidReferenceException: Expression site.shortName is undefined on line 9, column 56 in org/alfresco/repository/site/site.lib.ftl.

Caused by: freemarker.core.InvalidReferenceException: Expression site.shortName is undefined on line 9, column 56 in org/alfresco/repository/site/site.lib.ftl. at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125) at freemarker.core.Expression.getStringValue(Expression.java:118) at freemarker.core.AddConcatExpression._getAsTemplateModel(AddConcatExpression.java:98) at freemarker.core.Expression.getAsTemplateModel(Expression.java:89) at freemarker.core.ListLiteral.getModelList(ListLiteral.java:119) at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:91) at freemarker.core.Expression.getAsTemplateModel(Expression.java:89) at freemarker.core.Expression.getStringValue(Expression.java:93)

标签: rest alfresco
1条回答
The star\"
2楼-- · 2019-08-03 16:13

solved this little puzzler.

I had not added the content-type as "application/json" hence the request was barking up the wrong tree and going to ftl instead of json

following code works fine creating a site in alfresco.

    headers.set("Content-Type", "application/json");

        String objectToPost = "{\"shortName\":\"firm008\", \"title\":\"firm008\", \"description\":\"firm008\", \"visibility\":\"PRIVATE\", \"isPublic\":\"false\", \"sitePreset\":\"site-dashboard\"}";
    System.out.println(objectToPost);

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    **headers.set("Content-Type", "application/json");**

    HttpEntity<String> entity = new HttpEntity<String>(objectToPost,headers);

    String url = PRE_URL + "/alfresco/wcservice/api/sites"+"?alf_ticket="+ TICKET;
    String response = restTemplate.exchange(url,HttpMethod.POST, entity, String.class, new Object[] {}).toString();
    System.out.println("Created SITE response is =" + response);        
查看更多
登录 后发表回答