首页 > 解决方案 > spring cloud contract - 使用请求的主体作为响应主体

问题描述

在使用此合约的 Spring Cloud 合约 (v1.2.5) 中:

    Contract.make {
        description "update sthg"
        request {
            method PUT()
            url value($(consumer('/path/to/sthg'),
                    producer(execute('somePutPathPath(0,\"xxx\")'))))
            body(producer(
                    file('files/givens/my-body.json')
            ))
        }
        response {
            status 200
            headers {
                contentType(applicationJson())
            }
            body(fromRequest().body())
        }
    }

生成测试时出现错误

 org.springframework.cloud:spring-cloud-contract-maven-plugin:1.2.5.RELEASE:generateTests failed:net.minidev.json.parser.ParseException: Unexpected character ({) at position 1. -> [Help 1]

但如果我使用一切都很好:body(someProp: fromRequest().body())

    Contract.make {
        description "update sthg"
        request {
            method PUT()
            url value($(consumer('/path/to/sthg'),
                    producer(execute('somePutPathPath(0,\"xxx\")'))))
            body(producer(
                    file('files/givens/my-body.json')
            ))
        }
        response {
            status 200
            headers {
                contentType(applicationJson())
            }
            body(someProp: fromRequest().body())
        }
    }

我如何将正文响应设置为请求的正文。

标签: spring-cloudspring-cloud-contract

解决方案


我通过使用request.getBody()而不是:body(fromRequest().body())解决了这个问题

  body($(
            c(request.getBody()),
            p(file('some.json'))
    ))

推荐阅读