小编Eri*_*ley的帖子

QueryDsl Web查询Map字段的键

概观

特定

  • Spring Data JPA,Spring Data Rest,QueryDsl
  • 一个Meetup实体
    • 与一个Map<String,String> properties领域
      • MEETUP_PROPERTY作为一个坚持在一张桌子@ElementCollection
  • 一个 MeetupRepository
    • 延伸 QueryDslPredicateExecutor<Meetup>

我期待

一个Web查询

GET /api/meetup?properties[aKey]=aValue
Run Code Online (Sandbox Code Playgroud)

仅返回具有指定键和值的属性条目的Meetup:aKey = aValue.

但是,这对我不起作用.我错过了什么?

试着

简单的领域

简单字段起作用,如名称和描述:

GET /api/meetup?name=whatever
Run Code Online (Sandbox Code Playgroud)

与参与者一样,收集字段起作用

GET /api/meetup?participants.name=whatever
Run Code Online (Sandbox Code Playgroud)

但不是这个Map字段.

自定义QueryDsl绑定

我已经尝试通过拥有存储库来自定义绑定

extend QuerydslBinderCustomizer<QMeetup>
Run Code Online (Sandbox Code Playgroud)

并凌驾于

customize(QuerydslBindings bindings, QMeetup meetup)
Run Code Online (Sandbox Code Playgroud)

方法,但是当customize()方法被命中时,lambda内的绑定代码不是.

编辑:了解这是因为QuerydslBindings评估查询参数的方法不会让它与pathSpecs它内部持有的地图相匹配- 它有自定义绑定.

一些细节

Meetup.properties字段

@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "MEETUP_PROPERTY", joinColumns = @JoinColumn(name = "MEETUP_ID"))
@MapKeyColumn(name = "KEY")
@Column(name = "VALUE", length = 2048)
private Map<String, …
Run Code Online (Sandbox Code Playgroud)

java spring querydsl spring-data-jpa spring-data-rest

15
推荐指数
1
解决办法
1181
查看次数

由于spring-data-rest中的HAL"_links"元素导致restdocs SnippetException

我的应用正在使用spring-data-restspring-restdocs.我的设置非常标准; 几乎完全从文档中复制,但我已经包含了下面的示例,以防我遗漏了一些东西.当我的mvc测试运行时,它失败了:

org.springframework.restdocs.snippet.SnippetException: The following parts of the payload were not documented:
{
  "_links" : {
    "self" : {
      "href" : "https://my-api/item/10"
    },
    "item" : {
      "href" : "https://my-api/item/10"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的测试代码:

@Rule
public JUnitRestDocumentation restDocs = new JUnitRestDocumentation("target/generated-snippets");
// ...
mockMvc = webAppContextSetup(wac) //WebApplicationContext
        .apply(documentationConfiguration(restDocs)
                       .uris()
                       .withHost("my-api")
                       .withPort(443)
                       .withScheme("https"))
        .build();
// ....
mockMvc.perform(get("/items/{id}", "10"))
               .andDo(documentation)
Run Code Online (Sandbox Code Playgroud)

这是堆栈:

at org.springframework.restdocs.payload.AbstractFieldsSnippet.validateFieldDocumentation(AbstractFieldsSnippet.java:176)
at org.springframework.restdocs.payload.AbstractFieldsSnippet.createModel(AbstractFieldsSnippet.java:100)
at org.springframework.restdocs.snippet.TemplatedSnippet.document(TemplatedSnippet.java:64)
at org.springframework.restdocs.generate.RestDocumentationGenerator.handle(RestDocumentationGenerator.java:196)
at org.springframework.restdocs.mockmvc.RestDocumentationResultHandler.handle(RestDocumentationResultHandler.java:55)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:177)
at com.example.my.api.domain.MyRepositoryRestTest.findOne(MyRepositoryRestTest.java:36)
Run Code Online (Sandbox Code Playgroud)

我如何获得spring-restdocsspring-data-rest …

java spring spring-data-rest spring-restdocs

8
推荐指数
1
解决办法
1412
查看次数