6

I started on Magento2 Custom rest API module creation for that i follow the below post

API Module for Magneto2

url:- http://localhost/magento2a/rest/V1/calculator/add/1/2

app/code/AlanKent/CalculatorWebService/etc/module.xml

<?xml version="1.0"?>
<!--
/**
 * @copyright Copyright (c) 2015 X.commerce, Inc. (http://www.magentocommerce.com)
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="AlanKent_CalculatorWebService" setup_version="2.0.0"/>
</config>

app/code/AlanKent/CalculatorWebService/etc/di.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">

    <preference for="AlanKent\CalculatorWebService\Api\CalculatorInterface"
                type="AlanKent\CalculatorWebService\Model\Calculator" />

</config>

app/code/AlanKent/CalculatorWebService/etc/webapi.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">

    <!-- Example: curl http://127.0.0.1/index.php/rest/V1/calculator/add/1/2 -->
    <route url="/V1/calculator/add/:num1/:num2" method="GET">
        <service class="AlanKent\CalculatorWebService\Api\CalculatorInterface" method="add"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>

</routes>

app/code/AlanKent/CalculatorWebService/Api/CalculatorInterface.php

<?php

/**
 * Copyright 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace AlanKent\CalculatorWebService\Api;

use AlanKent\CalculatorWebService\Api\Data\PointInterface;

/**
 * Defines the service contract for some simple maths functions. The purpose is
 * to demonstrate the definition of a simple web service, not that these
 * functions are really useful in practice. The function prototypes were therefore
 * selected to demonstrate different parameter and return values, not as a good
 * calculator design.
 */
interface CalculatorInterface
{
    /**
     * Return the sum of the two numbers.
     *
     * @return int The sum of the numbers.
     */
    public function add($num1, $num2);
}

app/code/AlanKent/CalculatorWebService/Model/Calculator.php

<?php

/**
 * Copyright 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace AlanKent\CalculatorWebService\Model;

use AlanKent\CalculatorWebService\Api\CalculatorInterface;

/**
 * Defines the implementaiton class of the calculator service contract.
 */
class Calculator implements CalculatorInterface
{
    /**
     * Return the sum of the two numbers.
     *
     * @api
     * @param int $num1 Left hand operand.
     * @param int $num2 Right hand operand.
     * @return int The sum of the two values.
     */
    public function add($num1, $num2) {
        return $num1 + $num2;
    }
}

it work fine for me

Guys if you get error

 [2015-10-14 09:24:54] main.CRITICAL: exception 'InvalidArgumentException' with message 'Report ID: webapi-561e0346e13a1; Message: Each getter must have description with @return annotation. See Magento\Catalog\Api\ProductTypeListInterface::getTests()' in D:\xampp\htdocs\magento2a\lib\internal\Magento\Framework\Webapi\ErrorProcessor.php:194

then make sure in Interface file where you declare the function above that there should be @return in comments else it will through error

that is getTests its my new method aove that i have comment with @return if you missed the it will through the error

<?php
/**
 * Product type provider
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Catalog\Api;

/**
 * @api
 */
interface ProductTypeListInterface
{
    /**
     * Retrieve available product types
     *
     * @return \Magento\Catalog\Api\Data\ProductTypeInterface[]
     */
    public function getProductTypes();

    **/**
     * Retrieve available product types
     *
     * @return
     */**
    public function getTests();
}

it look wired but its works above is the solution for Error

main.CRITICAL: exception 'InvalidArgumentException' with message 'Report ID: webapi-561e0346e13a1; Message: Each getter must have description with @return annotation. 

Note:- once module is pasted dont forget to run upgrade command that is D:\xampp\php>php D:\xampp\htdocs\magento2a\bin\magento setup:upgrade

(in local system i have php in D drive so change as per your system)

1
  • Why do you have comment like ` **/**`?
    – KAndy
    Oct 14, 2015 at 7:53

2 Answers 2

4

The interface must have proper dock blocks with the existing types. But your code doesn't have a return type. For example:

<?php 
/**
 * Product type provider
 *
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Catalog\Api;

/**
 * @api
 */
interface ProductTypeListInterface
{
    /**
     * Retrieve available product types
     *
     * @return \Magento\Catalog\Api\Data\ProductTypeInterface[]
     */
    public function getProductTypes();

    /**
     * Retrieve available tests
     *
     * @return \Vendor\Module\Api\Data\TestInterface[]
     */
    public function getTests();
}
1
  • :- i just gave below code it works code<?php namespace AlanKent\CalculatorWebService\Api; use AlanKent\CalculatorWebService\Api\Data\PointInterface; interface CalculatorInterface { /** * Return the sum of the two numbers. * * @api * @param int $num1 * @param int $num2 * @return */ public function add($num1, $num2); } code Oct 14, 2015 at 9:09
2

@Kandy

i used below code it working fine for me important is dont ignore the comments which is consider as param

<?php


namespace AlanKent\CalculatorWebService\Api;

use AlanKent\CalculatorWebService\Api\Data\PointInterface;

interface CalculatorInterface
{
    /**
     * Return the sum of the two numbers.
     *
     * @api
     * @param int $num1
     * @param int $num2
     * @return
     */
    public function add($num1, $num2);
}

Your Answer

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

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