使用soapui模拟Rest服务响应(json和xml)

使用soapui模拟Rest服务响应(json和xml),json,rest,groovy,mocking,soapui,Json,Rest,Groovy,Mocking,Soapui,我感到困惑,因为我是模仿rest服务的新手 我正在使用soapui模拟rest服务。为此,我需要编写一个脚本(OnRequest脚本),以模拟对我的请求的响应 因此,我想模拟两种类型的响应:application/json和application/xml。但我不想读取任何响应文件。我只想在脚本(soapui中的OnRequest脚本)中编写响应 我已经找到了一种通过读取响应文件来实现的方法。在我的例子中,我不需要读取任何文件,而是将响应写入脚本(soapui中的OnRequest脚本) 有人说我

我感到困惑,因为我是模仿rest服务的新手

我正在使用soapui模拟rest服务。为此,我需要编写一个脚本(OnRequest脚本),以模拟对我的请求的响应

因此,我想模拟两种类型的响应:application/json和application/xml。但我不想读取任何响应文件。我只想在脚本(soapui中的OnRequest脚本)中编写响应

我已经找到了一种通过读取响应文件来实现的方法。在我的例子中,我不需要读取任何文件,而是将响应写入脚本(soapui中的OnRequest脚本)

有人说我可以像这样使用groovy:

if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
mockRunner.returnFile(httpResponse,new File(path + "test.xml"))
    log.info "Response returning for Content-Type application/xml"
    return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)    
}

但该示例确实读取了一个文件,我想使用groovy,但只是用groovy脚本编写答案。

我终于找到了解决方法:

if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
    WsdlMockResult mockResult = new WsdlMockResult(mockRequest)
    def httpResponse = mockRequest.httpResponse
    httpResponse.setContentType("application/xml;charset=utf-8")
    httpResponse.writer << "<root><user>ABC</user></root>"
    httpResponse.status = 200
    return mockResult
}
if(mockRequest.getMethod()=“GET”&&mediaType==“application/xml”){
WsdlMockResult mockResult=新的WsdlMockResult(mockRequest)
def httpResponse=mockRequest.httpResponse
httpResponse.setContentType(“application/xml;charset=utf-8”)

httpResponse.writer我终于找到了解决方法:

if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
    WsdlMockResult mockResult = new WsdlMockResult(mockRequest)
    def httpResponse = mockRequest.httpResponse
    httpResponse.setContentType("application/xml;charset=utf-8")
    httpResponse.writer << "<root><user>ABC</user></root>"
    httpResponse.status = 200
    return mockResult
}
if(mockRequest.getMethod()=“GET”&&mediaType==“application/xml”){
WsdlMockResult mockResult=新的WsdlMockResult(mockRequest)
def httpResponse=mockRequest.httpResponse
httpResponse.setContentType(“application/xml;charset=utf-8”)

httpResponse.writer你好,我终于找到了解决方法。你好,我终于找到了解决方法。