2016-12-02 27 views
1

次のRESTコントローラがあります。Spring Boot Data RestのHATEOASレスポンスへの追加情報

@RepositoryRestController 
@RequestMapping(value = "/booksCustom") 
public class BooksController extends ResourceSupport { 

    @Autowired 
    public BooksService booksService; 

    @Autowired 
    private PagedResourcesAssembler<Books> booksAssembler; 

    @RequestMapping("/search") 
    public HttpEntity<PagedResources<Resource<Books>>> search(@RequestParam(value = "q", required = false) String query, @PageableDefault(page = 0, size = 20) Pageable pageable) { 
     pageable = new PageRequest(0, 20); 

     Page<Books> booksResult = BooksService.findBookText(query, pageable); 

     return new ResponseEntity<PagedResources<Resource<Books>>>(BooksAssembler.toResource(BooksResult), HttpStatus.OK); 

    } 

マイPage<Books> BooksResult = BooksService.findBookText(query, pageable);SolrCrudRepositoryによって支えられています。それが実行されると、BookResultには複数のフィールドがあり、内容フィールドと他のいくつかのフィールド(highlighted)があります。残念ながら、私がREST応答から返すのは、contentフィールドのデータと、HATEOAS応答のメタデータ情報(ページ情報、リンクなど)のみです。応答にhighlightedフィールドを追加する適切な方法は何でしょうか?私はResponseEntityを修正する必要があると思っていますが、適切な方法は不明です。

編集

モデル:

@SolrDocument(solrCoreName = "Books_Core") 
public class Books { 
    @Field 
    private String id; 

    @Field 
    private String filename; 

    @Field("full_text") 
    private String fullText; 

    //Getters and setters omitted 
    ... 
} 

検索とSolrRepositoryが呼び出される(例えばBooksService.findBookText(クエリ、ページング可能);)私は戻って、これらのオブジェクトを取得します。

enter image description here

しかし、私のREST応答で私は、 "コンテンツ" を参照してください。 "強調表示された"オブジェクトをREST応答に追加できるようにしたいと思います。 HATEOASは "content"オブジェクト(オブジェクトについては以下を参照)に情報を送信するだけであるようです。

{ 
    "_embedded" : { 
    "solrBooks" : [ { 
     "filename" : "ABookName", 
     "fullText" : "ABook Text" 
    } ] 
    }, 
    "_links" : { 
    "first" : { 
     "href" : "http://localhost:8080/booksCustom/search?q=ABook&page=0&size=20" 
    }, 
    "self" : { 
     "href" : "http://localhost:8080/booksCustom/search?q=ABook" 
    }, 
    "next" : { 
     "href" : "http://localhost:8080/booksCustom/search?q=ABook&page=0&size=20" 
    }, 
    "last" : { 
     "href" : "http://localhost:8080/booksCustom/search?q=ABook&page=0&size=20" 
    } 
    }, 
    "page" : { 
    "size" : 1, 
    "totalElements" : 1, 
    "totalPages" : 1, 
    "number" : 0 
    } 
} 

完全な画像を得ることができるので、これはBooksServiceをサポートしているリポジトリです。すべてのサービスは、このSolrCrudRepositoryメソッドを呼び出します。

public interface SolrBooksRepository extends SolrCrudRepository<Books, String> { 

    @Highlight(prefix = "<highlight>", postfix = "</highlight>", fragsize = 20, snipplets = 3) 
    HighlightPage<SolrTestDocuments> findBookText(@Param("fullText") String fullText, Pageable pageable); 

} 
+1

何かがいるようだので、 'content'と' highlighted'の間で異なっていれば、 'Books'のソースコードを表示する必要があります。実際の結果の実際のスニペットと、あなたが期待する/望む結果が役立つかもしれません。 –

+0

変数名とフィールド名を小文字にしてください。大文字の大文字は、Java開発者のクラス名のように見えます。 –

+0

Booksエンティティクラスを投稿すると役に立ちます。 –

答えて

1

私は、応答ページを作成する代わりにHighlightPagePage<Books>を使用していました。ページには明らかに、強調表示された部分が切り捨てられていたcontentが含まれていません。私はHighlightPageに基づいて新しいページを作成し、その結果をPageの代わりに結果として返しました。

おそらく
@RepositoryRestController 
@RequestMapping(value = "/booksCustom") 
public class BooksController extends ResourceSupport { 

    @Autowired 
    public BooksService booksService; 

    @Autowired 
    private PagedResourcesAssembler<Books> booksAssembler; 

    @RequestMapping("/search") 
    public HttpEntity<PagedResources<Resource<HighlightPage>>> search(@RequestParam(value = "q", required = false) String query, @PageableDefault(page = 0, size = 20) Pageable pageable) { 

     HighlightPage solrBookResult = booksService.findBookText(query, pageable); 
     Page<Books> highlightedPages = new PageImpl(solrBookResult.getHighlighted(), pageable, solrBookResult.getTotalElements()); 
     return new ResponseEntity<PagedResources<Resource<HighlightPage>>>(booksAssembler.toResource(highlightedPages), HttpStatus.OK); 
    } 

これを行うには良い方法が、私はそれが変更にコードのトンを持たずにやりたいことだろう何かを見つけることができませんでした。お役に立てれば!

+0

ありがとうございますが、おそらく私たちには適していないでしょう。ありがとう! – Cipous

+0

問題ありません!問題をどのように解決するのか不思議です。がんばろう。 –

+0

私の解決策を追加しました。おそらくそれはあなたを助けるでしょう。 – Cipous

3

OK]をクリックして、ここで私はそれをやった方法です: 私は私が

public class HighlightPagedResources<R,T> extends PagedResources<R> { 

    private List<HighlightEntry<T>> phrases; 

    public HighlightPagedResources(Collection<R> content, PageMetadata metadata, List<HighlightEntry<T>> highlightPhrases, Link... links) { 
     super(content, metadata, links); 
     this.phrases = highlightPhrases; 
    } 

    @JsonProperty("highlighting") 
    public List<HighlightEntry<T>> getHighlightedPhrases() { 
     return phrases; 
    } 
} 

とHighlightPagedResourcesAssemblerをHighlightPagedResources書いた:

public class HighlightPagedResourcesAssembler<T> extends PagedResourcesAssembler<T> { 

    public HighlightPagedResourcesAssembler(HateoasPageableHandlerMethodArgumentResolver resolver, UriComponents baseUri) { 
     super(resolver, baseUri); 
    } 


    public <R extends ResourceSupport> HighlightPagedResources<R,T> toResource(HighlightPage<T> page, ResourceAssembler<T, R> assembler) { 
     final PagedResources<R> rs = super.toResource(page, assembler); 
     final Link[] links = new Link[rs.getLinks().size()]; 
     return new HighlightPagedResources<R, T>(rs.getContent(), rs.getMetadata(), page.getHighlighted(), rs.getLinks().toArray(links)); 
    } 
} 

私は私の春のRepositoryRestMvcConfigurationに追加する必要がありました。Javaの:cotrollerで

@Primary 
@Bean 
public HighlightPagedResourcesAssembler solrPagedResourcesAssembler() { 
    return new HighlightPagedResourcesAssembler<Object>(pageableResolver(), null); 
} 

私が新しく実装されたいずれかのPagedResourcesAssemblerを変更しても、リクエストメソッドに新しいHighlightPagedResourcesを使用する必要がありました:

@Autowired 
private HighlightPagedResourcesAssembler<Object> highlightPagedResourcesAssembler; 

@RequestMapping(value = "/conversations/search", method = POST) 

public HighlightPagedResources<PersistentEntityResource, Object> findAll(
     @RequestBody ConversationSearch search, 
     @SortDefault(sort = FIELD_LATEST_SEGMENT_START_DATE_TIME, direction = DESC) Pageable pageable, 
     PersistentEntityResourceAssembler assembler) { 

    HighlightPage page = conversationRepository.findByConversationSearch(search, pageable); 
    return highlightPagedResourcesAssembler.toResource(page, assembler); 
} 

結果:

{ 
    "_embedded": { 
    "conversations": [ 
    ..our stuff.. 
    ] 
    }, 
    "_links": { 
    ...as you know them... 
    }, 
    "page": { 
    "size": 1, 
    "totalElements": 25, 
    "totalPages": 25, 
    "number": 0 
    }, 
    "highlighting": [ 
    { 
     "entity": { 
     "conversationId": "a2127d01-747e-4312-b230-01c63dacac5a", 
     ... 
     }, 
     "highlights": [ 
     { 
      "field": { 
      "name": "textBody" 
      }, 
      "snipplets": [ 
      "Additional XXX License for YYY Servers DCL-2016-PO0422 \n  \n<em>hi</em> bodgan \n  \nwe urgently need the", 
      "Additional XXX License for YYY Servers DCL-2016-PO0422\n \n<em>hi</em> bodgan\n \nwe urgently need the permanent" 
      ] 
     } 
     ] 
    } 
    ] 
} 
関連する問題