You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RestAssured如何传递无引号的原始文本请求体?

RestAssured传递原始文本请求体去引号解决方案

两种直接有效的解决方式

  1. 用StringBody包装请求体
    显式声明内容为纯文本,避免被RestAssured默认的序列化逻辑添加引号:
import io.restassured.http.StringBody;

String command = "dumpsys package com.android.chrome | grep versionName";
request.pathParam("deviceId", deviceId);
request.contentType(ContentType.TEXT)
       .body(new StringBody(command));
  1. 指定文本类型的对象映射器
    通过重载的body方法传入文本专用映射器,跳过JSON序列化步骤:
String command = "dumpsys package com.android.chrome | grep versionName";
request.pathParam("deviceId", deviceId);
request.contentType(ContentType.TEXT)
       .body(command, new TextGsonObjectMapperFactory());

全局配置(可选)

如果项目频繁发送纯文本请求,可以全局配置RestAssured,让TEXT类型请求体直接传递原始内容:

RestAssured.config = RestAssured.config()
           .objectMapperConfig(new ObjectMapperConfig()
           .defaultObjectMapperType(ObjectMapperType.GSON)
           .textObjectMapperFactory((type, charset) -> new Gson()));

配置完成后,再使用contentType(ContentType.TEXT).body(command)就不会自动添加引号了。

内容的提问来源于stack exchange,提问作者user2416212

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 17:42:07