简体   繁体   中英

Spring Contracts: how to send a Collection of Strings as a RequestBody

A question about how to write a contract for a method annotated with @RequestBody taking a Collection of Strings as a parameter. I have the following method:

    @PostMapping(path = "/some/uri", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation("GET with body")
    public Response<Boolean> someMethod(@RequestParam(value = "key") final String key,
                                        @RequestBody final Collection<String> numbers){
        return some logic;
    }

and I have written the following contract for testing purposes:


import org.springframework.cloud.contract.spec.Contract


Contract.make {
    description "Should return true"
    request {
        method POST()
        url("/some/uri?key=NEW_KEY")
        body'''["12345",
                "00143"]'''
    }
    response {
        status 200
        headers {header 'Content-Type': 'application/json;charset=UTF-8'}
        body '''true'''
    }

I keep getting 415, the test cannot find my method, I guess my mistake might be in the way I send the collection of strings, I have tried some other options but did not succed.

I tried the suggestions above but unfortunately they both did not solve my issue. The reason I got 415 was that when I added a body to my request, the check was also made behind the scenes on the content type of the body, so I had to explicitly specify that the body was in json format also in the request:

request {
        method POST()
        url("/some/uri?key=NEW_KEY")
        headers {header 'Content-Type': 'application/json;charset=UTF-8'}
        body'''["12345",
                "00143"]'''
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM