2

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?

1 Answer 1

3

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);
}
4
  • 2
    @api can be ommited on the interface declaration, but added only on the function declarations. Dec 14, 2016 at 13:10
  • 1
    it's mean: if we put '@api' on head of interface class, all public functions will use as api methods. Is that right?
    – MrTo-Kane
    Dec 14, 2016 at 15:45
  • Let's me check and confirm again. Dec 14, 2016 at 15:46
  • 1
    Yes, all the public functions can be used as api methods. Dec 15, 2016 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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