Magento 2 – Create a Simple Custom API Module

apimagento2module

I have a problem when I create a new custom API module, my module is below:
enter image description here

Content of : CalculatorInterface.php

<?php

namespace Tohq\Webapi\Api;

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

Content of: Calculator.php

<?php

namespace Tohq\Webapi\Model;
use Tohq\Webapi\Api\CalculatorInterface;


class Calculator implements CalculatorInterface
{
    /**
     * Returns total of two numbers
     *
     * @api
     * @param int $num1
     * @param int $num2
     * @return int $result
     */
    public function cal($num1, $num2)
    {
        // TODO: Implement cal() method.
        $result =  $num1 + $num2;
        return $result;
    }
}

Content of 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/webapi/cal/:num1/:num2" method="GET">
        <service class="Tohq\Webapi\Api\CalculatorInterface" method="cal"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

Content of : 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="Tohq\Webapi\Api\CalculatorInterface" type="Tohq\Webapi\Model\Calculator" />
</config>

============== My issue below ==============
When I call Rest api : magento2.dev/rest/V1/webapi/cal/3/5
I got this issue:

This XML file does not appear to have any style information
associated with it. The document tree is shown below.

Class does not exist
-1

0 D:\xampp\htdocs\srcDebug\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(148):
ReflectionClass->__construct('') 1
D:\xampp\htdocs\srcDebug\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(322):
Magento\Framework\Webapi\ServiceInputProcessor->_createFromArray(NULL,
'3') 2
D:\xampp\htdocs\srcDebug\lib\internal\Magento\Framework\Webapi\ServiceInputProcessor.php(119):
Magento\Framework\Webapi\ServiceInputProcessor->convertValue('3',
NULL) 3
D:\xampp\htdocs\srcDebug\app\code\Magento\Webapi\Controller\Rest\InputParamsResolver.php(101):
Magento\Framework\Webapi\ServiceInputProcessor->process('Tohq\Webapi\Api…',
'cal', Array) 4
D:\xampp\htdocs\srcDebug\app\code\Magento\Webapi\Controller\Rest.php(299):
Magento\Webapi\Controller\Rest\InputParamsResolver->resolve() 5
D:\xampp\htdocs\srcDebug\app\code\Magento\Webapi\Controller\Rest.php(216):
Magento\Webapi\Controller\Rest->processApiRequest() 6
D:\xampp\htdocs\srcDebug\var\generation\Magento\Webapi\Controller\Rest\Interceptor.php(37):
Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
7 D:\xampp\htdocs\srcDebug\lib\internal\Magento\Framework\App\Http.php(135):
Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
8 D:\xampp\htdocs\srcDebug\lib\internal\Magento\Framework\App\Bootstrap.php(258):
Magento\Framework\App\Http->launch()
9
D:\xampp\htdocs\srcDebug\index.php(39):
Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
10 {main}

Anybody can see my issue and how to fix it?

Best Answer

Your interface needs to have the "annotation" @param and @return in your method. Can add @api at the first interface class or before method.

/**
*@api
*/
interface CalculatorInterface
{
    /**
     * Returns total of two numbers
     *
     * @param int $num1
     * @param int $num2
     * @return int $result
     */
     public function cal($num1, $num2);
}