簡體   English   中英

Liferay JSON Web服務:添加用戶無法正常工作?

[英]Liferay json web service: add-user not working?

我正在創建一個使用json Web服務連接到Liferay 6.2門戶的應用程序。 我已經配置了身份驗證參數,然后可以成功使用多個Web服務(即“ usergroup / add-user-group”可以正常工作)。

嘗試使用“ add-user”添加新用戶時,出現下一個錯誤:

"exception":"No JSON web service action associated with path /user/add-user and method POST for /"

這里我了解到,該錯誤可能是由於某些參數不正確或缺失,因此無法找到正確的Web服務,但是根據http://localhost:8080/api/jsonws的json規范,

 '/user/add-user' Parameters:

 companyId long
 autoPassword boolean
 password1 java.lang.String
 password2 java.lang.String
 autoScreenName boolean
 screenName java.lang.String
 emailAddress java.lang.String
 facebookId long
 openId java.lang.String
 locale java.util.Locale
 firstName java.lang.String
 middleName java.lang.String
 lastName java.lang.String
 prefixId int
 suffixId int
 male boolean
 birthdayMonth int
 birthdayDay int
 birthdayYear int
 jobTitle java.lang.String
 groupIds long[]
 organizationIds long[]
 roleIds long[]
 userGroupIds long[]
 sendEmail boolean
 serviceContext com.liferay.portal.service.ServiceContext 

為了訪問Web服務,我使用以下代碼(基於Liferay論壇 ):

public void serverConnection(String address, String protocol, int port, String webservicesPath, String loginUser, String password) {
    this.webservicesPath = webservicesPath;
    // Host definition
    targetHost = new HttpHost(address, port, protocol);
    // Credentials
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),
            new UsernamePasswordCredentials(loginUser, password));

    // Client
    httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();

    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicScheme = new BasicScheme();
    authCache.put(targetHost, basicScheme);
    // Add AuthCache to the execution context
    httpContext = new BasicHttpContext();
    httpContext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
}

這將創建用於連接和管理身份驗證的Http客戶端,並且:

public String getHttpResponse(String webService, List<NameValuePair> params) throws ClientProtocolException,
        IOException, NotConnectedToWebServiceException, AuthenticationRequired {
    // Set authentication param if defined.
    setAuthParam(params);

    HttpPost post = new HttpPost("/" + webservicesPath + webService);
    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
    post.setEntity(entity);
    HttpResponse response = getHttpClient().execute(targetHost, post, httpContext);
    if (response.getEntity() != null) {
        // A Simple JSON Response Read
        String result = EntityUtils.toString(response.getEntity());
        if (result.contains("{\"exception\":\"Authenticated access required\"}")) {
            throw new AuthenticationRequired("Authenticated access required.");
        }
        return result;
    }
    return null;
}

那將調用網絡服務並讀取響應。 然后,對於調用,我執行兩個操作:連接和調用Web服務。 現在連接很簡單:

serverConnection("localhost", "http", 8080, "api/jsonws/", "test@liferay.com", "test");

調用“添加用戶” Web服務:

    public User addUser(Company company, String password, String screenName, String emailAddress, long facebookId,
        String openId, String locale, String firstName, String middleName, String lastName, int prefixId,
        int sufixId, boolean male, int birthdayDay, int birthdayMonth, int birthdayYear, String jobTitle,
        long[] groupIds, long[] organizationIds, long[] roleIds, long[] userGroupIds, boolean sendEmail)
        throws NotConnectedToWebServiceException, ClientProtocolException, IOException, AuthenticationRequired, WebServiceAccessError {
    checkConnection();
    boolean autoPassword = false;
    boolean autoScreenName = false;
    if (password == null || password.length() == 0) {
        autoPassword = true;
    }
    if (screenName == null || screenName.length() == 0) {
        autoScreenName = true;
    }

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("companyId", Long.toString(company.getCompanyId())));
    params.add(new BasicNameValuePair("autoPassword", Boolean.toString(autoPassword)));
    params.add(new BasicNameValuePair("password1", password));
    params.add(new BasicNameValuePair("password2", password));
    params.add(new BasicNameValuePair("autoScreenName", Boolean.toString(autoScreenName)));
    params.add(new BasicNameValuePair("screenName", screenName));
    params.add(new BasicNameValuePair("emailAddress", emailAddress));
    params.add(new BasicNameValuePair("facebookId", Long.toString(facebookId)));
    params.add(new BasicNameValuePair("openId", openId));
    params.add(new BasicNameValuePair("locale", locale));
    params.add(new BasicNameValuePair("firstName", firstName));
    params.add(new BasicNameValuePair("middleName", middleName));
    params.add(new BasicNameValuePair("lastName", lastName));
    params.add(new BasicNameValuePair("prefixId", Integer.toString(prefixId)));
    params.add(new BasicNameValuePair("sufixId", Integer.toString(sufixId)));
    params.add(new BasicNameValuePair("male", Boolean.toString(male)));
    params.add(new BasicNameValuePair("birthdayMonth", Integer.toString(birthdayMonth));
    params.add(new BasicNameValuePair("birthdayDay", Integer.toString(birthdayDay)));
    params.add(new BasicNameValuePair("birthdayYear", Integer.toString(birthdayYear)));
    params.add(new BasicNameValuePair("jobTitle", jobTitle));
    params.add(new BasicNameValuePair("groupIds", Arrays.toString(groupIds)));
    params.add(new BasicNameValuePair("organizationIds", Arrays.toString(organizationIds)));
    params.add(new BasicNameValuePair("roleIds", Arrays.toString(roleIds)));
    params.add(new BasicNameValuePair("userGroupIds", Arrays.toString(userGroupIds)));
    params.add(new BasicNameValuePair("sendEmail", Boolean.toString(sendEmail)));
    params.add(new BasicNameValuePair("serviceContext", "{}"));

    String result = getHttpResponse("user/add-user", params);
    User user = null;
    if (result != null) {
        // A Simple JSON Response Read
        user = decodeFromJason(result, User.class);
        userPool.addUser(user);
        LiferayClientLogger.info(this.getClass().getName(), "User '" + user.getScreenName() + "' added.");
        return user;
    }

    return user;
}

被稱為:

 //company is a Liferay company instance with not null value. 
 addUser(company, "testpass", "testUser", "mail@mail.com", 0, "", "es_ES", "testUser", "testUser", "testUser", 0, 0, true, 1, 1, 1900, "Tailor", null, null, null, null, false);

基本上,這將創建所有參數並調用Web服務。 我認為所有參數都與Web服務期望的參數完全匹配。 那么問題是:

什么意思得到的錯誤? 盡管參數正確,仍可能出現? 如果參數不正確,那是正確的參數嗎?

替換為:

params.add(new BasicNameValuePair(“ sufixId”,Integer.toString(sufixId)));

有了這個:

params.add(new BasicNameValuePair(“ suffixId”,Integer.toString(sufixId)));

並刪除此:

params.add(new BasicNameValuePair(“ serviceContext”,“ {}”)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM