使用apijsonwsuserupdate-user更新Liferay用户。

问题描述 投票:0回答:1

无法使用JAVA远程调用更新用户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("[email protected]", "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","[email protected]"));
        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();
            }
        }
    }

}
}
java rest liferay
1个回答
0
投票

你可以使用提示,基于你试图调用的方法的参数计数。请注意,你必须在调用中指定每个参数。

https:/help.liferay.comhcen-usarticles360018151631-JSON-Web服务#using-hints。

© www.soinside.com 2019 - 2024. All rights reserved.