Magento 2 : WebAPI gives error: Class "array" does not exist

If you've added proper docblock and still getting this error, replace @param array to @param string[] or @param mixed[] as according to magento devdocs.

Valid scalar types include: mixed (or anyType), bool (or boolean), str (or string), integer (or int), float, and double.

Also note that you must always use fully qualified class name or a fully qualified interface name in docblock.

Reference :

https://devdocs.magento.com/guides/v2.2/extension-dev-guide/service-contracts/service-to-web-service.html


When ever you are using Rest API in Magento 2. You have to follow these steps :

  1. Create an interface file Api/TestApiManagementInterface.php and define all the api methods you want.
  2. Create Model file Model/Api/TestApiManagement.php to implement all the methods defined in TestApiManagementInterface.php.
  3. Create a etc/webapi.xml and defines the routes and their permissions.
  4. Create etc/di.xml and add preferences for you interface and model.

While creating interface and model, we define parameters DocBlock annotation and return DocBlock annotations in both file and it should match in bothe the file. Example given below :

  1. Interface :
    interface LoginInterface {
  /**
     * Login email.
     *
     * @param string $email
     * @param string $password
     * @return string
     */
  public function login($email,$password);
}
  1. Model :
 class Login implements LoginInterface {

 /**
 * Returns greeting message to user
 * @author : Arman Khan
 *
 * @api
 * @param string $email Users email.
 * @param string $password Users password.
 * @return string Greeting message with users response.
 */   
 public function login($email,$password)  { 
    //..............
 }

If you are miss matching parameters data type in DocBlock annotation of either interface or model you can face this error "Class “array” does not exist"

OR

If you are passing wrong parameter's data type in postman or API call you will face this 'Class “array” does not exist issue.

So check parameter's data type every where and it should be same.

Check screenshot for the same error I faced as it entered wrong data type in interface annotation

Screenshot for same error

I got this error when i changed datatype of parameter from string to array in interface :

    interface LoginInterface {
  /**
     * Login email.
     *
     * @param string $email
     * @param array $password
     * @return string
     */
  public function login($email,$password);
}

I hope this will help you to figure out your issue.

Tags:

Api

Magento2