Saya telah membuat tes layanan web Magento 2.0.2 REST berdasarkan blog Alan: http://alankent.me/2015/07/24/creating-a-new-rest-web-service-in-magento-2/
Saya menggunakan tukang pos untuk memanggil layanan web kustom dan mendapatkan kesalahan berikut:
"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}"
Saya dapat berhasil memanggil layanan web REST Magento di luar kotak.
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 yang mengembalikan kesalahan:
http://local.magento202.com:81/index.php/rest/V1/calculator/add/30/70
sumber
Dalam masalah kasus saya adalah bahwa saya menggunakan klausul "gunakan" dalam antarmuka. Magento DocBlockReflection tidak bisa mengatasinya dan sedang mencari antarmuka tanpa namespace lengkap. Jadi misalnya dalam kode di bawah ini:
Saya perlu menghapus klausul "gunakan":
sumber
Pastikan perintah di bawah ini berhasil dijalankan. Jangan menyela atau menekan panggilan API apa pun. Setelah eksekusi, masalah Anda akan teratasi. Bekerja untukku.
sumber