掲示板

Liferay 6.2 JSONWS Custom Classes

thumbnail
6年前 に Rodrigo Mantica によって更新されました。

Liferay 6.2 JSONWS Custom Classes

New Member 投稿: 6 参加年月日: 17/05/29 最新の投稿
Hey all.

I've successfully generated custom services using ServiceBuilder for remote use. I can see most of my custom methods at /jsonws/api/. However, it seems that any public method that has inputStream and ServiceContext parameters are ignored by ServiceBuilder. I have tried adding annotations to manually select that method for building but no luck.

Any ideas on how to debug?

Regards,
Rodrigo Mantica
thumbnail
6年前 に Djamel TORCHE によって更新されました。

RE: Liferay 6.2 JSONWS Custom Classes

New Member 投稿: 18 参加年月日: 14/10/21 最新の投稿
Hi,
Web services methods are intended to be invoked externally and in standard way, that means that it can be invoked for example by JavaScript code or any other programming language!, So you can not send specific Java stuff. i-e From client side you send only texts and Files.

PS: Yes that there are some Liferay specific possibilities like having methods expecting Objects (parametres with plus sign and indicating the implementation classe, or sending a Map as json object).

Kind regards,
Djamel
thumbnail
6年前 に Rodrigo Mantica によって更新されました。

RE: Liferay 6.2 JSONWS Custom Classes

New Member 投稿: 6 参加年月日: 17/05/29 最新の投稿
Djamel TORCHE:
Hi,
Web services methods are intended to be invoked externally and in standard way, that means that it can be invoked for example by JavaScript code or any other programming language!, So you can not send specific Java stuff. i-e From client side you send only texts and Files.

PS: Yes that there are some Liferay specific possibilities like having methods expecting Objects (parametres with plus sign and indicating the implementation classe, or sending a Map as json object).

Kind regards,
Djamel


Hey Djamel,

Thank you for your response. I will try to remove any java-specific parameters from my public methods. I've been trying to implement the file uploading functionality found in the guide "Invoking JSON Web Services". Their multi-part form looks like this:

<form action="http://localhost:8080/api/jsonws/dlapp/add-file-entry" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="repositoryId" value="10172">
    <input type="hidden" name="folderId" value="0">
    <input type="hidden" name="title" value="test.jpg">
    <input type="hidden" name="description" value="File upload example">
    <input type="hidden" name="changeLog" value="v1">
    <input type="file" name="file">
    <input type="submit" value="addFileEntry(file)">
</form>


This HTML format did not work for me on the client-side. I tried my own implementation that looks like this:
uploadFile(file, project) {
    console.log("uploading file in chat service:");
    console.log(file);
    const self = this;
    return new Promise((resolve, reject) =&gt; {
      var headers = this.userService.headers;
      headers.append('Content-Type', 'text/plain; charset=utf-8');
      headers.append("enctype", "multipart/form-data");
      let now = new Date();
      const endpoint = this.userService.api + "/dlapp/add-file-entry.9/repositoryId/" + 20484 + "/folderId/" + 0 + "/title/" + now.getTime() + ".jpg";
      var formData = new FormData();
      formData.append('file', file);
      this.http.post(endpoint, formData, {headers})
      .map(res =&gt; res.json())
      .subscribe(data =&gt; {
        console.log("upload returned response:");
        console.log(data);
        resolve(data);
      });
    });
  }


The code seems to work, but the file is not being uploaded with the request. My console logs say the file entry was created but the file size is 0. Any ideas?
thumbnail
6年前 に Djamel TORCHE によって更新されました。

RE: Liferay 6.2 JSONWS Custom Classes

New Member 投稿: 18 参加年月日: 14/10/21 最新の投稿
Hi,
First of all and regarding the web service at /dlapp/add-file-entry, sincerly I never used them directly, I have my own methods (code based on service builder for example) then I invoke from my own code the add-file-entry, below some code snippets that can help you:

@JSONWebService(method = "POST")
public dataImport(File file, long siteGroupId){
String ext = FilenameUtils.getExtension(file.getName());
System.out.println(" --------&gt;file: " + file.getAbsolutePath());
System.out.println("            -&gt;ext : " + ext);
//To get InputStream , just :
InputStream is = new FileInputStream(file);
//To get bytes array
byte[] bytes = IOUtils.toByteArray(is);

//To get mimetype dynamicaly :
String mimeType = MimeTypesUtil.getContentType(file);

//To get user Request :
userName = getGuestOrUser().getFullName() 

//Then to use liferay services:
DLAppServiceUtil.addFileEntry(repositoryId, folderId, file.getName(), mimeType, fileName, "File imported by " + userName + ", operation date : " + dateToDisplay, null, bytes, serviceContext);
}


For ServiceContext, just get by:


ServiceContext serviceContext = new ServiceContext();
serviceContext.setScopeGroupId(siteGroupId);
// permissions for files and directories added
serviceContext.setAddGroupPermissions(true);

For folders, root folder id = repositoryId and = groupId (site id), so very simple to add files to site root folder, if you have subfolders just get their IDs by their unique names by DLAppServiceUtil.getFolder() , use groupId as parent dir Id.

Second thing, As I see you use AngularJs, for that I recommend you to use third part libraries like angular-file-upload, here you find demo (click on edit form to see code). For example for my case I have many methods based on Service Builder that expecting files (pdf, pictures etc) and one of the clients side is a mobile app based on IONIC (witch is based on Angular), I use FileTransfer without any issues (with progress counting). Same web services also invoked from Java desktop app using Apache HttpClient whitout any problem also (if you need help in this case don't hesitate to ask).

Kind regards,
Djamel
thumbnail
6年前 に Rodrigo Mantica によって更新されました。

RE: Liferay 6.2 JSONWS Custom Classes

New Member 投稿: 6 参加年月日: 17/05/29 最新の投稿
Hey Djamel,

First off, thank you so much you've been very helpful with this issue.

I will try to implement the angular module to see if that solves my issue. I'm using Ionic 3/Angular 4 at the moment. Do you happen to have an example of this for those versions? If so, I would really love to see your implementation of FileTransfer and/or angular-http-file-upload.

Regards,
Rodrigo

EDIT:

My previous uploadFile method was almost there. I just needed to remove the line
headers.append('Content-Type', 'text/plain; charset=utf-8');


Thank you so much!!