Ibm mobilefirst MobileFirst-调用Java SQL适配器过程

Ibm mobilefirst MobileFirst-调用Java SQL适配器过程,ibm-mobilefirst,mobilefirst-adapters,Ibm Mobilefirst,Mobilefirst Adapters,我正在学习MobileFirstPlatform 7的教程 我试图使用userId=“bjones”获取用户,但我不知道如何将参数{userId}设置到过程/adapters/UserAdapter/{userId}中 function loadUsers(){ busyIndicator.show(); var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET

我正在学习MobileFirstPlatform 7的教程

我试图使用userId=“bjones”获取用户,但我不知道如何将参数
{userId}
设置到过程
/adapters/UserAdapter/{userId}

function loadUsers(){
  busyIndicator.show();

  var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/", WLResourceRequest.GET);
  resourceRequest.setQueryParameter("userId", "bjones");
  resourceRequest.send().then(
        loadUsersSuccess,
        loadUsersFailure
);}

function loadUsersSuccess(result){
  WL.Logger.debug("Feed retrieve success");
  busyIndicator.hide();
  WL.Logger.debug(JSON.stringify(result));
  if (result.responseJSON.length>0) 
    displayFeeds(result.responseJSON);
  else 
    loadUsersFailure();}

function loadUsersFailure(result){
  WL.Logger.error("Feed retrieve failure");
  busyIndicator.hide();
  WL.SimpleDialog.show("Banking Application", "Service not available. Try again later.", 
        [{
            text : 'Reload',
            handler : WL.Client.reloadApp 
        },
        {
            text: 'Close',
            handler : function() {}
        }]
    );}
我的要求是

localhost:10080/JavaAdapters/adapters/UserAdapter/?userId=bjones

但是JSON响应包含存储在我的数据库中的所有用户


另外,REST调用类型
@PUT
,带有路径参数“userId”和主体参数:“firstName”、“lastName”、“password”,为了更新教程中的用户,适配器端点是
/{userId}
,这意味着
userId
不是查询参数,而是url的一部分。您需要更新
loadUsers
函数,以便它在url末尾追加
userId
,因此在您的示例中,完整路径将是
/adapters/UserAdapter/bjones

function loadUsers(){
  busyIndicator.show();

  var usedId = "bjones";

  var resourceRequest = new WLResourceRequest("/adapters/UserAdapter/"+userId, WLResourceRequest.GET);
  resourceRequest.send().then(loadUsersSuccess,loadUsersFailure);

}
更新:

function loadUsersSuccess(result) {
  WL.Logger.debug("Feed retrieve success");
  busyIndicator.hide();
  WL.Logger.debug(JSON.stringify(result));
  // if responseJSON is not null user data was returned
  if (result.responseJSON != null) {
    displayFeeds(result.responseJSON);
  } else{
    loadUsersFailure();
  }
}

基本上有两种带参数的URL类型: 1.路径参数: /适配器/UserAdapter/users/{userId} 2.查询参数: /适配器/UserAdapter/users?userId={userId}

带有查询参数的java适配器:

@GET
@Produces("application/json")
@OAuthSecurity(enabled = false)
@Path("/users")

public String getuserById(@QueryParam("userID") String userId)

{
           System.out.println(userId);
}
@GET
@Produces("application/json")
@OAuthSecurity(enabled = false)
@Path("/users/{userId}")

public String getuserById(@PathParam("userId") String userId)

{
           System.out.println(userId);
} 
带有路径参数的java适配器:

@GET
@Produces("application/json")
@OAuthSecurity(enabled = false)
@Path("/users")

public String getuserById(@QueryParam("userID") String userId)

{
           System.out.println(userId);
}
@GET
@Produces("application/json")
@OAuthSecurity(enabled = false)
@Path("/users/{userId}")

public String getuserById(@PathParam("userId") String userId)

{
           System.out.println(userId);
} 

我希望第二个示例能够回答您在java适配器中的问题。

如何定义LoadUsersAccess函数,因为现在result.responseJSON.length尚未定义。“responseJSON”:{“lastName”:“Jones”,“userId”:“bjones”,“firstName”:“Brad”,“password”:“bjones”}检查答案的更新。有很多其他的方法可以做到这一点