07-11-2020 10:21 PM
How can the client code calling Java-backed webscript's execute method get a return value, because the execute method returns void? The method is GET.
I have Java-backed webscript code similar to:
public void execute(WebScriptRequest request, WebScriptResponse response) throws IOException{ // some more code to get argument from the URL int numOfFiles = myClass.getCountOfFiles(..some args..) } The above webscript is called by a client code similar to: try { URL url = new URL("https://myhost:myport/alfresco/service/fileNum"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); String output; while ((output = br.readLine()) != null) { System.out.println(output); } conn.disconnect(); }catch(Exception e) { logger.error("Exception in callling web service" + e.toString()); }
Inside the execute method, I indeed know the num files. But how do I propagate that value to the response obtained in the Client? The output received by the client can be a simple String value (e.g. 30 where 30 is the numOfFiles), and it does not have to be JSON.
Do I need to write any additional code inside execute method to propagate the value of numOfFiles to the client code? And if yes, how to get that value of numOfFiles in the client code?
Is there a way to add values in the WebScriptResponse inside execute, which can be obtained by the client code?
07-11-2020 11:06 PM
Creating a java backed WebScript by extending AbstractWebScript class gives similar flavor as Http Servlet. You have request and response objects available to process the request and write the result using response object.
To write something to WebScript response you can call write method by getting the instance or Writer.
response.getWriter().write("hello")
It can be used to write any type of response such as text, xml, json, html etc. considering you prepare the response within WebScript and write it.
On the other hand their is another implementation of Java backed WebScript which called "DeclarativeWebScript" which you can extend to return a model (basically a java map) object.
This mode object will be used to pouplate the response in any of the extpected format based on your implementation.
Check these posts for more details:
Alfresco Javabacked Webscripts
Explore our Alfresco products with the links below. Use labels to filter content by product module.