我想向标准Intershop ProductResource引入新的子资源 而不会失去产品资源的环境。
例如,在我的资源代码中,我想了解REST客户端引用的产品/product/SOME/my-sub-resource
我该怎么做?
答案 0 :(得分:1)
“ Cookbook-REST Framework”中有一个食谱:
向现有REST API添加资源 https://support.intershop.com/kb/index.php/Display/28269L
答案 1 :(得分:1)
<implementation name="YOUR_NAME"
implements="AbstractRestResource"
class="YOUR_FQNAME_TO_IMPL_CLASS"
factory="JavaBeanFactory">
<requires name="name" contract="String" cardinality="1..1" />
<requires name="subResource" contract="RestResource" cardinality="0..n" />
</implementation>
的新Java类<instance with="YOUR_NAME" name="YOUR_INSTANCE_NAME">
<fulfill requirement="name" value="YOUR_SUBRESOUCE_NAME" />
</instance>
subResource
intershop.WebShop.RESTAPI.ProductResource
添加到<fulfill requirement="subResource"
of="intershop.WebShop.RESTAPI.ProductResource"
with="YOUR_INSTANCE_NAME"/>
实例/product/SKU/YOUR_SUBRESOURCE_NAME
之后,您的资源在javax.ws.rs.GET
下可用。确保您的impl类具有公共方法,并且该方法使用javax.ws.rs.Produces
和@GET
@Produces("application/json")
public SomeRO get()
{
ApplicationBO applicationBO = provider.get();
ProductBORepository productBORep = applicationBO.getRepository(ProductBORepositoryExtension.EXTENSION_ID);
ProductBO product = productBORep.getProductBOBySKU(getParent().getName());
// Do the stuff you want with the product
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication106
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
XDocument doc = XDocument.Parse(xml);
XElement root = doc.Root;
XNamespace ns = root.GetDefaultNamespace();
XElement siteDataResult = doc.Descendants(ns + "SiteDataResult").FirstOrDefault();
XNamespace aNs = siteDataResult.GetNamespaceOfPrefix("a");
Site site = new Site();
site.siteName = (string)siteDataResult.Element(aNs + "SiteName");
site.siteLocation = (string)siteDataResult.Element(aNs + "SiteLocation");
site.dict = siteDataResult.Descendants(aNs + "DateIntervalNode")
.GroupBy(x => (DateTime)x.Element(aNs + "Date"), y => new KeyValuePair<int, string>(
(int)y.Descendants(aNs + "AverageTemperature").FirstOrDefault(),
(string)y.Descendants(aNs + "Unit").FirstOrDefault()
)
)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
}
}
public class Site
{
public string siteName { get; set; }
public string siteLocation { get; set; }
public Dictionary<DateTime, KeyValuePair<int, string>> dict { get; set; }
}
}