Spring Cloud Contract-使用请求的主体作为响应主体

时间:2018-10-25 12:43:25

标签: spring-cloud spring-cloud-contract

使用此合同的春季云合同(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())
        }
    }

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

1 个答案:

答案 0 :(得分:0)

我使用 request.getBody()代替了 body(fromRequest()。body())

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