问题描述
我想在我的请求字段文档中使用Dto模型的约束。当我使用自动休息文档时,它可以工作,但是KeycloakAuthMock却有问题。因此,我不会根据可以轻松添加requestfields的文档使用自动休息文档。
@Test
@Order(5)
@WithMockKeycloakAuth(authorities = "ROLE_owner")
void createProduct_RealmRoleOwner_HttpStatusCreated() throws Exception {
MvcResult response = mockmvc.perform(post(URL).contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsstring(testProductDto))
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.created").isNumber())
.andExpect(jsonPath("$.name").value(testProductDto.getName()))
.andExpect(jsonPath("$.price").value(testProductDto.getPrice()))
.andExpect(jsonPath("$.id").value(1L))
.andDo(document("{methodName}",Preprocessors.preprocessRequest(),Preprocessors.preprocessResponse(
ResponseModifyingPreprocessors.replaceBinaryContent(),ResponseModifyingPreprocessors.limitJsonArrayLength(objectMapper),Preprocessors.prettyPrint()),requestFields( fieldWithPath("name").description("Name of the product"),fieldWithPath("price").description("Price of the new product")),responseFields(fieldWithPath("id").description("Id of the new product"),fieldWithPath("name").description("Name of the product"),fieldWithPath("price").description("Price of the new product"),fieldWithPath("created").description("Unix timestamp when the product was created")
)))
.andReturn();
型号:
@ApiModel(description = "Data transfer model for a new product.")
public class ProductDto {
@ApiModelProperty(notes = "Name of the new product.")
@NotNull(message = "name must not be null")
@Size(min = 2,max = 10,message = "Name length min is 2 and max is 10")
private String name;
@ApiModelProperty(notes = "price of the new product")
@NotNull(message = "price must not be null")
@Min(value = 1,message = "price min is 1")
@Max(value = 100,message = "price max is 100")
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() { return price; }
public void setPrice(Double price) { this.price = price; }
}
我尝试遵循guide,并添加了template。使用了模板,但出现了Moustache异常,未找到约束字段。因为我不会手工记录约束,所以我可以让谁让Spring Rest文档添加模型提供的约束?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)