在 spring colud contract 中定义请求主体
define request body in spring colud contract
我想在生产者 API 中使用 spring 云合约编写合约。
这是我的 API:
@PostMapping(path = "/person",
produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<PersonInfo> test(@RequestBody PersonRequest request){
PersonInfo personInfo = bankService.getPersonInfo(request);
return new ResponseEntity<>(personInfo, HttpStatus.OK);
}
我的 DSL 在 groovy 中:
Contract.make {
description("spring cloud contract")
request {
method 'POST'
url '/person'
headers {
contentType(applicationJson())
}
body("""
{
"nationalId": "2548745"
}
""")
}
response {
status 200
body("""
{
"name": "Marc",
"family": "Brown",
"gender": "M"
}
""")
headers {
contentType(applicationJson())
}
}
}
个人请求:
public class PersonRequest {
private String nationalId;
public String getNationalId() {
return nationalId;
}
public void setNationalId(String nationalId) {
this.nationalId = nationalId;
}
}
现在,当我清理并安装项目时出现此错误:
期待:
<415>
等于:
<200>
但不是。
如果我将 @RequestBody PersonRequest request
更改为 @RequestBody String request
一切正常。
我正在使用 spring boot 2.3.1.RELEASE 和 spring-cloud-starter-contract-verifier 2.2.3.RELEASE 和 junit5
我用junit4测试了这个case,没问题。我该如何解决这个问题?
不改dsl,改baseclass
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig() .encoderConfig(new EncoderConfig(UTF_8.name(), UTF_8.name()));
或选择不附加默认内容字符集:
EncoderConfig encoderConfig = new EncoderConfig() .appendDefaultContentCharsetToContentTypeIfUndefined(false); RestAssuredMockMvcConfig restAssuredConf = new RestAssuredMockMvcConfig() .encoderConfig(encoderConfig); RestAssuredMockMvc.config = restAssuredConf;
我想在生产者 API 中使用 spring 云合约编写合约。
这是我的 API:
@PostMapping(path = "/person",
produces = {MediaType.APPLICATION_JSON_VALUE}, consumes = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<PersonInfo> test(@RequestBody PersonRequest request){
PersonInfo personInfo = bankService.getPersonInfo(request);
return new ResponseEntity<>(personInfo, HttpStatus.OK);
}
我的 DSL 在 groovy 中:
Contract.make {
description("spring cloud contract")
request {
method 'POST'
url '/person'
headers {
contentType(applicationJson())
}
body("""
{
"nationalId": "2548745"
}
""")
}
response {
status 200
body("""
{
"name": "Marc",
"family": "Brown",
"gender": "M"
}
""")
headers {
contentType(applicationJson())
}
}
}
个人请求:
public class PersonRequest {
private String nationalId;
public String getNationalId() {
return nationalId;
}
public void setNationalId(String nationalId) {
this.nationalId = nationalId;
}
}
现在,当我清理并安装项目时出现此错误:
期待: <415> 等于: <200> 但不是。
如果我将 @RequestBody PersonRequest request
更改为 @RequestBody String request
一切正常。
我正在使用 spring boot 2.3.1.RELEASE 和 spring-cloud-starter-contract-verifier 2.2.3.RELEASE 和 junit5
我用junit4测试了这个case,没问题。我该如何解决这个问题?
不改dsl,改baseclass
RestAssuredMockMvc.config = new RestAssuredMockMvcConfig() .encoderConfig(new EncoderConfig(UTF_8.name(), UTF_8.name()));
或选择不附加默认内容字符集:
EncoderConfig encoderConfig = new EncoderConfig() .appendDefaultContentCharsetToContentTypeIfUndefined(false); RestAssuredMockMvcConfig restAssuredConf = new RestAssuredMockMvcConfig() .encoderConfig(encoderConfig); RestAssuredMockMvc.config = restAssuredConf;