15

I've created a test Magento 2.0.2 REST web service based on Alan's blog: http://alankent.me/2015/07/24/creating-a-new-rest-web-service-in-magento-2/

I'm using Postman to call the custom web service and get the following error:

"message": "Class  does not exist",
  "code": -1,
  "trace": "#0 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\framework\\Webapi\\ServiceInputProcessor.php(128): ReflectionClass->__construct('')\n#1 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\framework\\Webapi\\ServiceInputProcessor.php(262): Magento\\Framework\\Webapi\\ServiceInputProcessor->_createFromArray(NULL, '30')\n#2 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\framework\\Webapi\\ServiceInputProcessor.php(99): Magento\\Framework\\Webapi\\ServiceInputProcessor->convertValue('30', NULL)\n#3 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\module-webapi\\Controller\\Rest.php(262): Magento\\Framework\\Webapi\\ServiceInputProcessor->process('Test\\\\Calculator...', 'add', Array)\n#4 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\module-webapi\\Controller\\Rest.php(160): Magento\\Webapi\\Controller\\Rest->processApiRequest()\n#5 P:\\wwwroot\\Magento202_com_loc\\Web\\var\\generation\\Magento\\Webapi\\Controller\\Rest\\Interceptor.php(24): Magento\\Webapi\\Controller\\Rest->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#6 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\framework\\App\\Http.php(115): Magento\\Webapi\\Controller\\Rest\\Interceptor->dispatch(Object(Magento\\Framework\\App\\Request\\Http))\n#7 P:\\wwwroot\\Magento202_com_loc\\Web\\vendor\\magento\\framework\\App\\Bootstrap.php(258): Magento\\Framework\\App\\Http->launch()\n#8 P:\\wwwroot\\Magento202_com_loc\\Web\\index.php(39): Magento\\Framework\\App\\Bootstrap->run(Object(Magento\\Framework\\App\\Http))\n#9 {main}"

I can successfully call Magento out-of-the-box REST web services.

app/code/Test/Calculator/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Test_Calculator',
__DIR__
);

app/code/Test/Calculator/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Test_Calculator" setup_version="1.0.0"/>
</config>

app/code/Test/Calculator/etc/webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">

    <route url="/V1/calculator/add/:num1/:num2" method="GET">
        <service class="Test\Calculator\Api\CalculatorInterface" method="add"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

app/code/Test/Calculator/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Test\Calculator\Api\CalculatorInterface" type="Test\Calculator\Model\Calculator" />
</config>

app/code/Test/Calculator/Api/CalculatorInterface.php

<?php

namespace Test\Calculator\Api;

interface CalculatorInterface
{
    public function add($num1, $num2);
}

app/code/Test/Calculator/Model/Calculator.php

<?php

namespace Test\Calculator\Model;

use Test\Calculator\Api\CalculatorInterface;

class Calculator implements CalculatorInterface
{
    public function add($num1, $num2) {
        return $num1 + $num2;
    }
}

REST url that returns the error:

http://local.magento202.com:81/index.php/rest/V1/calculator/add/30/70
33

A DocBlock is required in app/code/Test/Calculator/Api/CalculatorInterface.php as described here: http://devdocs.magento.com/guides/v2.0/coding-standards/docblock-standard-general.html

<?php

namespace Test\Calculator\Api;

interface CalculatorInterface
{
    /**
     * Add two numbers.
     *
     * @param int $num1
     * @param int $num2
     * @return int
     */
    public function add($num1, $num2);
}
5
  • 1
    I was receiving the the same error, because i used @params instead of "@param". Magento 2 is too strict in code standards :P Aug 25 '16 at 10:48
  • what should i write as return value if i want return json array Dec 20 '16 at 12:10
  • [at]return array @Bhupendra Jadeja
    – Ying Style
    Sep 12 '17 at 14:27
  • @AltafHussain I know it has been 2 years, but it has nothing to do with coding standards, is uses reflection to do validation, so it it can't find "@param" it will just break point blank. It's like you set $a and later want to it to use as $b but php standards are too strict
    – DarkMukke
    Oct 16 '18 at 15:15
  • You, sir, are my hero. Thank you. I would have never thought that would have mattered but it fixed my problem. May 2 '19 at 18:30
10

In my case problem was that I used "use" clausule in interface. Magento DocBlockReflection could not handle that and was searching for interface without full namespace. So for example in bellow code:

use My\Namespace\ExampleObjectInterface
interface ExampleObjectRepositoryInterface
{
/**
 * xyz
 * @param int $id
 * @return ExampleObjectInterface
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function getById($id);
}

I needed to remove "use" clausule:

interface ExampleObjectRepositoryInterface
{
/**
 * xyz
 * @param int $id
 * @return \My\Namespace\ExampleObjectInterface
 * @throws \Magento\Framework\Exception\NoSuchEntityException
 * @throws \Magento\Framework\Exception\LocalizedException
 */
public function getById($id);
}
2
  • 2
    omg, you have saved my life. I was debugging this for hours. Why is magento framework so hard to use :(
    – Alex
    Mar 7 '20 at 22:44
  • Insane this was my case also. surely it should be valid with Aliases! Jun 9 '20 at 9:52
2

Make sure below command executed successfully. Don't interrupt or hit any API call. After execution your issue will be resolved. Worked for me.

php bin/magento setup:di:compile
2

Here's the checklist if you struggling with same issue:

  1. Service interfaces should be in Api folder.
  2. Dto interfaces should be in Api/Data folder.
  3. All interface methods should be type hinted. (Argument types, return types)
  4. Imports should not be used. Specify a full namespace path for class.
  5. ALL interfaces should have the correct PHPDOC.

Hope it helps

1

In my case, It seemed that Magento DocBlockReflection could not find the API interface and class since it was in the root folder of my Plugin, I added my interface in the Api folder and my class in the Model folder and it worked!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.