Liferay使用/ api / jsonws / user / update-user更新用户

时间:2018-05-04 07:43:25

标签: java rest liferay

无法使用JAVA远程调用update-user api。

{
  "exception": "No JSON web service action with path /user/update-user and method POST for null",
  "throwable": "com.liferay.portal.kernel.jsonwebservice.NoSuchJSONWebServiceException: No JSON web service action with path /user/update-user and method POST for null",
  "error": {
    "message": "No JSON web service action with path /user/update-user and method POST for null",
    "type": "com.liferay.portal.kernel.jsonwebservice.NoSuchJSONWebServiceException"
  }
}
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class TestClass{

public static void main(String[] args) {

    CloseableHttpClient httpClient = null;
    try {

        HttpHost targetHost = new HttpHost("localhost", 8080, "http");
        CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),new UsernamePasswordCredentials("test@liferay.com", "test"));
        httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
        AuthCache authCache = new BasicAuthCache();
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(targetHost, basicAuth);
        BasicHttpContext ctx = new BasicHttpContext();
        ctx.setAttribute(ClientContext.AUTH_CACHE, authCache);

        RequestConfig config = RequestConfig.custom().build();
        HttpPost post = new HttpPost("/api/jsonws/user/update-user");
        post.setConfig(config);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("userId", "61761"));
        params.add(new BasicNameValuePair("oldPassword ", "Test123q"));
        params.add(new BasicNameValuePair("passwordReset", Boolean.toString(false))); // autogenerating the password
        params.add(new BasicNameValuePair("newPassword1", "Test123a"));
        params.add(new BasicNameValuePair("newPassword2", "Test123a"));
        params.add(new BasicNameValuePair("reminderQueryQuestion", "First Car")); 
        params.add(new BasicNameValuePair("reminderQueryAnswer", "Tesla")); 
        params.add(new BasicNameValuePair("screenName", "hulk"));
        params.add(new BasicNameValuePair("emailAddress","hulk@del.com"));
        params.add(new BasicNameValuePair("facebookId", "0"));
        params.add(new BasicNameValuePair("openId",null));
        params.add(new BasicNameValuePair("languageId", "en_US"));
        params.add(new BasicNameValuePair("timeZoneId", "UTC"));
        params.add(new BasicNameValuePair("greeting", "Hello Test Userr"));
        params.add(new BasicNameValuePair("comments", ""));
        params.add(new BasicNameValuePair("firstName","Hulk"));
        params.add(new BasicNameValuePair("middleName","M"));
        params.add(new BasicNameValuePair("lastName","User"));
        params.add(new BasicNameValuePair("prefixId", "1"));
        params.add(new BasicNameValuePair("suffixId", "0"));
        params.add(new BasicNameValuePair("birthdayMonth", "1"));
        params.add(new BasicNameValuePair("birthdayDay", "1"));
        params.add(new BasicNameValuePair("birthdayYear", "2000"));
        params.add(new BasicNameValuePair("male", "true")); 
        params.add(new BasicNameValuePair("jobTitle", ""));
        params.add(new BasicNameValuePair("groupIds", null));
        params.add(new BasicNameValuePair("organizationIds", null));
        params.add(new BasicNameValuePair("roleIds", null));
        params.add(new BasicNameValuePair("userGroupIds", null));
        params.add(new BasicNameValuePair("userGroupRoles",null ));
        params.add(new BasicNameValuePair("smsSn",""));
        params.add(new BasicNameValuePair("facebookSn",""));
        params.add(new BasicNameValuePair("jabberSn",""));
        params.add(new BasicNameValuePair("skypeSn", ""));
        params.add(new BasicNameValuePair("twitterSn",""));
        params.add(new BasicNameValuePair("serviceContext", "{}"));



        //params.add(new BasicNameValuePair("addresses",null));
        //params.add(new BasicNameValuePair("emailAddresses",null));
        //params.add(new BasicNameValuePair("phones",null));
        //params.add(new BasicNameValuePair("websites",null));
        //params.add(new BasicNameValuePair("announcementsDelivers",null));

        //params.add(new BasicNameValuePair("sendEmail", "false")); // no email will be triggered
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
        post.setEntity(entity);

        HttpResponse resp = httpClient.execute(targetHost, post, ctx);
        HttpEntity ent = resp.getEntity();

        // Post Processing need to be done
        String result = EntityUtils.toString(ent);
        System.out.println(result);
        JSONObject jsonObj=new JSONObject(result);
        System.out.println("Woooooo");

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        if (null != httpClient) {
            try {
                httpClient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
}

1 个答案:

答案 0 :(得分:0)

您可以根据您尝试调用的方法的参数计数来使用提示。请注意,您必须在调用中指定每个参数:

https://help.liferay.com/hc/en-us/articles/360018151631-JSON-Web-Services#using-hints