1

我在露天创建了一个多合一放大器。在 repo-amp 内部我创建了一个 java webscript,我想将文件名作为特定工作区的 json 返回。下面的java代码

.
.
.

public class Fields extends AbstractWebScript
{

      private ServiceRegistry serviceRegistry;

    public void setServiceRegistry(ServiceRegistry serviceRegistry) {
        this.serviceRegistry = serviceRegistry;
    }
    public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException
    {
        try
        {
            NodeService nodeService = serviceRegistry.getNodeService();
            String parameter=req.getParameter("nodeRef");
            NodeRef nodeRef=new NodeRef(parameter);

               Map<QName, Serializable> props = nodeService.getProperties(nodeRef);

   // Fetch a few common properties
   String name = (String)props.get(ContentModel.PROP_NAME);
   String title = (String)props.get(ContentModel.PROP_TITLE);

JSONObject obj = new JSONObject();

            // put some data on it
            obj.put(title,name);
            /
            // build a JSON string and send it back
            String jsonString = obj.toString();
            res.getWriter().write(jsonString);
        }
        catch(JSONException e)
        {
            throw new WebScriptException("Unable to serialize JSON");
        }
    }

在 webscript-context.xml 中定义脚本如下

    <bean id="webscript.fields.get" 
      class="com.test.testa.demoamp.Fields"
      parent="webscript">

</bean>

我创建了一个配置 xml 文件 fields.get.desc.xml

<webscript>
  <shortname>Fields</shortname>
  <description>Hands back a little bit of JSON</description>
  <url>/api/fields?nodeRef={nodeRef}</url>
   <authentication>user</authentication>
    <format default="json"></format>
   <property name="serviceRegistry" ref="ServiceRegistry" />
</webscript>

当我从浏览器执行脚本时,出现以下错误

http://localhost:8080/alfresco/service/api/fields?nodeRef=workspace://SpacesStore/f96e024a-0a13-4bf9-905e-2ae3f5bcf450

{
    "status" : 
  {
    "code" : 500,
    "name" : "Internal Error",
    "description" : "An error inside the HTTP server which prevented it from fulfilling the request."
  },  

  "message" : "java.lang.NullPointerException",  
  "exception" : "java.lang.NullPointerException",

  "callstack" : 
  [ 
      ""      ,"java.lang.NullPointerException"
      ,"com.test.testa.demoamp.Fields_docx$$EPf3jZCD.execute(Fields_docx.java:94)"
      ,"com.test.testa.demoamp.Fields_docx$$DPf3jZCD.execute(Unknown Source)"
      ,"com.test.testa.demoamp.Fields_docx.execute(Fields_docx.java)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:482)"
      ,"org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:457)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:551)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:619)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScriptInternal(RepositoryContainer.java:399)"
      ,"org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:280)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:378)"
      ,"org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)"
      ,"org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:132)"
      ,"javax.servlet.http.HttpServlet.service(HttpServlet.java:728)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)"
      ,"org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)"
      ,"org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)"
      ,"org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)"
      ,"org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)"
      ,"org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)"
      ,"org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)"
      ,"org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)"
      ,"org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)"
      ,"org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)"
      ,"org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)"
      ,"org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)"
      ,"org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)"
      ,"org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)"
      ,"org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)"
      ,"org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)"
      ,"java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)"
      ,"java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)"
      ,"java.lang.Thread.run(Thread.java:745)"

  ],

  "server" : "Community v5.0.0 (d r99759-b2) schema 8,022",
  "time" : "Mar 10, 2016 6:12:10 PM"
}

我尝试了各种类型的身份验证,但它们都不起作用。缺少什么并且 noderef 为 null ?

4

1 回答 1

4

您的 bean 定义需要更新以将 serviceregistry 注入您的 bean

 <bean id="webscript.fields.get" 
  class="com.test.testa.demoamp.Fields"
  parent="webscript">
    <property name="serviceRegistry" ref="ServiceRegistry"/>
 </bean>

希望这可以解决您的问题。

对于每个依赖注入,您需要在上下文文件中引用条目,并使用 getter setter 将 bean 声明为属性。

于 2016-03-12T06:30:05.337 回答