1

我创建了一个自定义 POST api,用于在 MCS 中获取登录信息。当我签入 SOAPUI 时,它工作得非常好。传递的参数是

1. header   
Oracle-Mobile-Backend-Id: ********************
2. Authentocation
Username:****************
password: **************

和基本登录信息用户名和密码分别为“User1”和“user1”。

第2步:

当我从 MAF 调用 API 时出现错误 400,使用的 post 方法是

    public static Response callPost(String restURI, String jsonRequest) {
    String responseJson = "";
    Response response = new Response();

    RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter();
    restServiceAdapter.clearRequestProperties();
    //restServiceAdapter.setConnectionName("MiddlewareAPI");
   // restServiceAdapter.setConnectionName("");


    restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_POST);
    restServiceAdapter.addRequestProperty("Content-Type", "application/json");
    restServiceAdapter.addRequestProperty("Accept", "application/json; charset=UTF-8");
    restServiceAdapter.addRequestProperty("Oracle-Mobile-Backend-Id", "**********");
    restServiceAdapter.addRequestProperty("Domain", "mcsdem0001");


    restServiceAdapter.addRequestProperty("Username", "******");
    restServiceAdapter.addRequestProperty("Password", "*****");
    //restServiceAdapter.addRequestProperty("Authorization", "Basic "+new String(encodedBytes));
    System.out.println("**** Authorization String ****=>"+new String(encodedBytes));
    System.out.println("**** RestURI ******=>"+restURI);
    System.out.println("**** jsonRequest ******=>"+jsonRequest);

    restServiceAdapter.setRequestURI(restURI);
    restServiceAdapter.setRetryLimit(0);

    try {
        responseJson = restServiceAdapter.send(jsonRequest);
        int responseCode = restServiceAdapter.getResponseStatus();
        response.setResponseCode(responseCode);
        response.setResponseMessage(responseJson);
        response.setHeader(restServiceAdapter.getResponseHeaders());
    } catch (Exception e) {
        int responseCode = restServiceAdapter.getResponseStatus();
        response.setResponseCode(responseCode);
        response.setResponseMessage(responseJson);
    }
    System.out.println("Response:" + responseJson);

    return response;
}

谁能告诉我post方法有什么错误吗?

4

3 回答 3

1

实际上这一点

restServiceAdapter.addRequestProperty("用户名", "******"); restServiceAdapter.addRequestProperty("密码", "*****");

不起作用,因为您尝试将用户名和密码作为 HTTP 标头传递。相反,它应该在您在这里尝试时通过

restServiceAdapter.addRequestProperty("授权", "基本"+new String(encodedBytes));

但是,这些不应是编码字节,而是格式为 base64 编码的字符串

基础(没有 < abd >)

请注意,仅需要在多租户环境中提供用户身份域。在 MCS 中,用户域是通过您连接到的移动后端定义的。

坦率

于 2015-12-14T07:18:55.983 回答
1

这可能是由于版本冲突。尝试使用HttpUrlConnection而不是RestServiceAdapter让我知道它是否有效。

于 2016-03-23T04:46:19.647 回答
0

使用 MAF MCS 实用程序库使其更容易分配。可在此处找到开发人员指南:http: //download.oracle.com/otn_hosted_doc/maf/mafmcsutility-api-doc-082015.pdf

示例代码:

MBEConfiguration mbeConfiguration = 
    new MBEConfiguration(
          <mbe rest connection>,<mobileBackendId>,
          <anonymous key string>,<application key string>, 
          MBEConfiguration.AuthenticationType.BASIC_AUTH);
 mbeConfiguration.setEnableAnalytics(true);
 mbeConfiguration.setLoggingEnabled(false)
 mbeConfiguration.setMobileDeviceId(
         DeviceManagerFactory.getDeviceManager().getName());
 MBE mobileBackend = MBEManager.getManager().
         createOrRenewMobileBackend(<mobile backend Id>, mbeConfiguration);

CustomAPI customApiProxy = mbe.getServiceProxyCustomApi();
MCSRequest request = new MCSRequest(mobileBackend.getMbeConfiguration());

request.setConnectionName(<Rest connection name>);
request.setRequestURI("/moile/custom/mockup/employees");
request.setHttpMethod(MCSRequest.HttpMethod.POST);
request.setPayload("{\"id\":\"1\"\"name\":\"nimphius\",\"firstName\":\"frank\"}");
request.setRetryLimit(0);
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/json");
request.setHttpHeaders(headers);
MCSResponse response = customApiProxy .sendForStringResponse(request);
String jsonResponse = (String) response.getMessage();
于 2015-09-22T08:01:46.600 回答