SpringBoot集成腾讯云敏感词校验API流程

1.pom.xml中引入腾讯云jar配置信息

XML 复制代码
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>4.0.11</version>
</dependency>

2.application.yaml中添加配置

java 复制代码
tencent:
  tms:
    secret-id: AKDahCRQ9bolQovlKccMpB
    secret-key: tWswkHXWEenfCpCI7fxxhA
    biz-type: test_demo

3.项目中映射配置信息

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "tencent.tms")
public class TencentTmsProperties {
    private String secretId;
    private String secretkey;
    private String bizType;
}

4.封装敏感词校验工具类

java 复制代码
@Slf4j
@Component
public class TencentTmsUtils {
    @Autowired
    private TencentTmsProperties tencentTmsProperties;

    /**
     * 敏感词过滤
     *
     * @param content
     * @return
     */
    public Boolean getTmsResult(String content) {
        if (StringUtils.isEmpty(content) || StringUtils.isEmpty(content.trim())) {
            return Boolean.TRUE;
        }
        Credential cre = new Credential(tencentTmsProperties.getSecretId(), tencentTmsProperties.getSecretkey());
        TmsClient client = new TmsClient(cre, "ap-shanghai");
        TextModerationRequest request = new TextModerationRequest();
        request.setBizType(tencentTmsProperties.getBizType());
        request.setContent(Base64.encode(content));
        try {
            TextModerationResponse response = client.TextModeration(request);
            if (!ObjectUtils.isEmpty(response)) {
                return "Pass".equals(response.getSuggestion());
            }
            //response.getLabel();//返回值:Normal:正常,Porn:色情,Abuse:谩骂,Ad:广告,Custom
            //response.getSuggestion();//返回值:Block:建议屏蔽,Review :建议人工复审,Pass:建议通过
            log.info("敏感词类型:" + response.getLabel() + "处理结果:" + response.getSuggestion());
        } catch (TencentCloudSDKException e) {
            log.error("Sensitive word detection failed, because: ", e);
        }
        return Boolean.FALSE;
    }
}

5.业务代码中应用敏感词校验

java 复制代码
 // 获取评论内容信息
 String content = req.getCommentContent();
 if (!tencentTmsUtils.getTmsResult(content)) {
    throw new BusinessException("评论内容包含敏感词语");
 }
相关推荐
ZhengEnCi16 分钟前
J7A-高级Java工程师面试三道灵魂拷问-深度广度与工程素养的终极检验
java·后端
爱勇宝2 小时前
小红花成长新版:模板来了,鼓励也更容易开始
前端·后端·程序员
用户47949283569152 小时前
翻完 lark-cli 的 17 万行 Go 代码,我学到了什么
后端·openai
卷无止境3 小时前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
羑悻3 小时前
别再只接个 API 了!我用 EdgeOne Makers 手搓了一个“懂业务”的官网售前 AI
后端
卷无止境3 小时前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
程序员威哥3 小时前
零基础玩转西门子PLC:C#手撕S7协议,打造工业数据采集神器
后端
用户742837256333 小时前
【Ambari Plus】Step9—AmbariServer 初始化
后端
wuxinzhe76cmd3 小时前
JVM 垃圾回收基础:从 STW 到分代收集(附 G1/ZGC 导读)
后端
MrSYJ4 小时前
TCP协议理解
后端·tcp/ip