You have to use a data interface in order to return the value:
In your interface Vendor\Namespace\Api\ColorInterface change the return value of the function name to Vendor\Namespace\Api\Data\ColorInterface and add the data interface in the file Vendor/Namespace/Api/Data/ColorInterface.php:
<?php namespace Vendor\Namespace\Api\Data; interface ColorInterface { const NAME = 'name'; const LNAME = 'lname'; /** * Get name * @return string|null */ public function getName(); /** * Set name * @param string $name * @return \Vendor\Namespace\Api\Data\ColorInterface */ public function setName($name); /** * Get lname * @return string|null */ public function getLname(); /** * Set lname * @param string $lname * @return \Vendor\Namespace\Api\Data\ColorInterface */ public function setLname($lname); }
Then add a Data Model for the recently added interface Vendor/Namespace/Model/Data/Color.php:
<?php namespace Vendor\Namespace\Model\Data; use Magento\Framework\Model\AbstractExtensibleModel; use Vendor\Namespace\Api\Data\ColorInterface; class Color extends AbstractExtensibleModel implements ColorInterface { /** * Get name * @return string|null */ public function getName() { return $this->getData(self::NAME); } /** * Set name * @param string $name * @return \Vendor\Namespace\Api\Data\ColorInterface */ public function setName($name) { return $this->setData(self::NAME, $name); } /** * Get lname * @return string|null */ public function getLname() { return $this->_get(self::LNAME); } /** * Set lname * @param string $lname * @return \Vendor\Namespace\Api\Data\ColorInterface */ public function setLname($lname) { return $this->setData(self::LNAME, $lname); } }
Then add the preference for the recently added interface Vendor/Namespace/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="\Vendor\Namespace\Api\Data\ColorInterface" type="Vendor\Namespace\Model\Data\Color" /> </config>
Finally inject the Data Interface and return it in your file:
<?php namespace Vendor\NameSpace\Model; use Vendor\NameSpace\Api\ColorInterface; use Vendor\Namespace\Api\Data\ColorInterface as DataColorInterface; class Color implements ColorInterface { /** * @var DataColorInterface */ private $dataColor; /** * Constructor. * @param HelperStock $helperStock */ public function __construct( DataColorInterface $dataColor ) { $this->dataColor = $dataColor; } /** * Returns greeting message to user * * @api * @param string $name Users name. * @return \Vendor\Namespace\Api\Data\ColorInterface */ public function name() { $dataColor = clone $this->dataColor; $dataColor->setName("Jose"); $dataColor->setLname("Sell"); return $dataColor; } }
webapi.xml??