rest-Assured-解析json错误-需使用预定义的解析器解析

报错信息:

java.lang.IllegalStateException: Expected response body to be verified as JSON, HTML or XML but content-type 'text/plain' is not supported out of the box.
Try registering a custom parser using:
RestAssured.registerParser("text/plain", <parser type>);

解决方案:

1.使用预定义解析器:RestAssured.registerParser("text/plain", Parser.JSON);


public static void registerParser(String  contentType, Parser  parser)
使用预定义的解析器注册要解析的自定义内容类型。例如,您希望使用XML解析器解析内容类型应用程序/自定义,以便能够使用XML点符号来验证响应:
 get(“/ x”)。then()。assertThat()。body(“document.child”,equalsTo(“something”))..
 
由于默认情况下,由于应用程序/自定义未被注册为由XML解析器处理,因此您需要在发出请求之前明确地告诉REST Assured使用此解析器:
 RestAssured.registerParser(“application / custom,Parser.XML”);
 
参数:
contentType - 要注册的内容类型
parser - 验证响应时使用的解析器。
原文地址:https://www.cnblogs.com/lin-123/p/7110282.html