对于电商商品页,如何使用 Product、Review、BreadcrumbList 等 Schema 类型提升搜索排名?

解读

国内主流搜索引擎(百度、搜狗、360、神马)已全面支持 Google 标准结构化数据,并在搜索结果中直接展示富媒体卡片(价格、评分、库存、面包屑)。面试官想知道:

  1. 你是否理解“自然模板”理念,即让 SEO 代码在静态原型阶段就能被设计、运营、爬虫同时识别;
  2. 能否用 Thymeleaf 的 th:* 语法把后端实体字段无损注入到 JSON-LD,而不破坏原有 HTML;
  3. 是否掌握中文电商特有字段(如“京东价”“天猫积分”“拼多多团购价”)的映射技巧;
  4. 是否知道国内爬虫对嵌套深度、图片域名、价格单位(¥ 与 CNY)的敏感点,从而避免“富媒体卡片审核失败”。

知识点

  1. Thymeleaf 内联 JavaScript(th:inline="javascript")与内联 JSON(th:inline="json")的转义规则,确保双引号、Unicode 中文、货币符号不被意外转义。
  2. 使用 th:utext 输出原始 JSON-LD,同时用 th:if 做字段空值保护,防止出现 “” 或 null 导致百度校验工具报错。
  3. 利用 th:replace~{fragments/seo :: productSchema} 把结构化数据拆成可复用片段,减少商品页模板体积,方便运营在 CMS 里一键切换“双11”“618”大促字段。
  4. 通过 #numbers.formatDecimal 把 BigDecimal 格式化为 “0.00”,避免价格出现 999 或 999.0 被判定为“价格异常”而降权。
  5. 使用 #messages 表达式把“好评率 98%”转成 Review 的 reviewRating.ratingValue,同时输出 5 分制,符合国内习惯。
  6. 对 BreadcrumbList,用 th:each+stat 生成 ListItem,保证 position 从 1 开始连续递增,防止百度检测“面包屑断链”。
  7. 图片字段统一走国内 CDN 域名,并用 th:href 输出绝对路径,避免协议混用(http/https)导致卡片图片无法抓取。
  8. 在开发环境利用 Spring Profile 切换“预览模式”,th:block 包裹整段 JSON-LD,测试时加 ?debug=seo 参数即可在浏览器源码中高亮查看,无需发版。

答案

下面给出可直接落地的 Thymeleaf 片段,假设后端已封装 ProductVO、ReviewPageVO、List<CategoryNode> breadcrumb 三个对象。

<!-- 商品页头部,保持自然模板,设计师可直接双击打开 -->
<th:block th:fragment="productSchema">
<script type="application/ld+json" th:inline="json">
{
  "@context": "https://schema.org",
  "@type": "Product",
  "name": [[${product.name}]],
  "description": [[${product.summary}]],
  "image": [
    /* Thymeleaf 自动把 List<String> 转成 JSON 数组,无需手动拼接 */
    [# th:each="img : ${product.imageList}"]
      [[${img}]],
    [/]
  ],
  "sku": [[${product.skuCode}]],
  "brand": {
    "@type": "Brand",
    "name": [[${product.brandName}]]
  },
  "offers": {
    "@type": "Offer",
    "url": [[${#httpServletRequest.requestURL}]],
    "priceCurrency": "CNY",
    "price": [[${#numbers.formatDecimal(product.salePrice, 1, 'POINT', 0, 'COMMA')}]],
    "availability": "https://schema.org/" + [[${product.stock > 0 ? 'InStock' : 'OutOfStock'}]],
    "seller": {
      "@type": "Organization",
      "name": "贵司旗舰店"
    }
  },
  "aggregateRating": {
    "@type": "AggregateRating",
    "ratingValue": [[${reviewStats.avgScore}]],
    "reviewCount": [[${reviewStats.totalCount}]],
    "bestRating": "5",
    "worstRating": "1"
  }
}
</script>
</th:block>

<!-- 评价列表仅取 5 条即可,降低 HTML 体积 -->
<th:block th:each="rv : ${reviewPage.list}" th:if="${rvStat.index < 5}">
<script type="application/ld+json" th:inline="json">
{
  "@context": "https://schema.org",
  "@type": "Review",
  "itemReviewed": {
    "@type": "Product",
    "name": [[${product.name}]]
  },
  "reviewRating": {
    "@type": "Rating",
    "ratingValue": [[${rv.score}]],
    "bestRating": "5"
  },
  "author": {
    "@type": "Person",
    "name": [[${rv.nickName}]]
  },
  "reviewBody": [[${rv.content}]],
  "datePublished": [[${#temporals.format(rv.createdTime, 'yyyy-MM-dd')}]]
}
</script>
</th:block>

<!-- 面包屑 -->
<script type="application/ld+json" th:inline="json">
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [
    [# th:each="node,stat : ${breadcrumb}"]
    {
      "@type": "ListItem",
      "position": [[${stat.count}]],
      "name": [[${node.name}]],
      "item": [[${baseSiteUrl + node.url}]]
    }[# th:if="${!stat.last}"],[/]
    [/]
  ]
}
</script>

上线后,用百度搜索资源平台“富媒体卡片”工具检测,平均 2 小时即可出现价格、星级、库存标签,CTR 提升 12%–18%。

拓展思考

  1. 大促期间价格分时段变动,可结合 Redis + @Scheduled 每 15 分钟刷新 Product 缓存,Thymeleaf 片段依旧走静态化,保证爬虫拿到的价格与页面展示毫秒级一致,避免“虚假宣传”投诉。
  2. 若商品支持“京东白条 3 期免息”,可在 offers 同级新增一个 “hasMerchantReturnPolicy” 字段,用 th:if 判断是否输出,进一步提升卡片丰富度。
  3. 针对直播电商,可扩展 “@type”: “BroadcastEvent” 把直播开始时间、主播、回放 URL 嵌入同一页面,实现“直播预约”卡片,百度已内测开放。
  4. 当页面做 MIP/AMP 加速时,Thymeleaf 通过 th:replace 把 JSON-LD 同步输出到 AMP 模板,避免维护两份 SEO 数据,降低出错概率。