Java Liferay json web服务:添加不工作的用户?

Java Liferay json web服务:添加不工作的用户?,java,json,web-services,liferay,Java,Json,Web Services,Liferay,我正在创建一个使用json web服务连接到Liferay 6.2门户的应用程序。 我已经配置了身份验证参数,然后可以成功地使用多个web服务(即“usergroup/addusergroup”工作正常) 尝试使用“添加用户”添加新用户时,出现下一个错误: "exception":"No JSON web service action associated with path /user/add-user and method POST for /" 正如我从中了解到的,此错误可能是因为某些参

我正在创建一个使用json web服务连接到Liferay 6.2门户的应用程序。 我已经配置了身份验证参数,然后可以成功地使用多个web服务(即“usergroup/addusergroup”工作正常)

尝试使用“添加用户”添加新用户时,出现下一个错误:

"exception":"No JSON web service action associated with path /user/add-user and method POST for /"
正如我从中了解到的,此错误可能是因为某些参数不正确或丢失,因此无法找到正确的web服务,但根据
json规范http://localhost:8080/api/jsonws

 '/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服务,我使用以下代码(基于):

创建用于连接和管理身份验证的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服务:

    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;
}
这基本上会创建所有参数并调用web服务。我认为所有参数都与web服务期望的参数完全匹配。那么问题是:

什么是获得的误差?尽管参数正确,是否仍可能出现? 如果参数不正确,请选择正确的参数?

更换:

params.add(新的BasicNameValuePair(“sufixId”,Integer.toString(sufixId))

为此:

add(新的BasicNameValuePair(“suffixId”,Integer.toString(sufixId))

并删除以下内容:


add(新的BasicNameValuePair(“serviceContext”,“{}”)

如果从web界面使用,则web服务可以正常工作。我从未尝试过此方法,但您可以提供serviceContext值作为空值。谢谢您的回答。对于“null”值,观察到相同的错误。错误一定在别处。是的,我刚刚测试过,你是对的。我太专注于服务结构了,以至于看不到“suffixId”上的打字错误。谢谢你指出!
serverConnection("localhost", "http", 8080, "api/jsonws/", "test@liferay.com", "test");
    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);