コントローラーに次のようなものがあります。
@RequestMapping
@ResponseBody
public HttpEntity<PagedResources<PromotionResource>> promotions(
@PageableDefault(size = RestAPIConfig.DEFAULT_PAGE_SIZE, page = 0) Pageable pageable,
PagedResourcesAssembler<Promotion> assembler
){
PagedResources<PromotionResource> r = assembler.toResource(this.promoService.find(pageable), this.promoAssembler);
return new ResponseEntity<PagedResources<PromotionResource>>(r, HttpStatus.OK);
}
そのコントローラ メソッドにマップされた URL に移動すると、次の根本原因で 500 エラーが発生します。
com.sun.istack.internal.SAXException2: unable to marshal type "commerce.api.rest.resources.PromotionResource " as an element because it is missing an @XmlRootElement annotation
リソースに @XmlRootElement アノテーションをスローすると、次のエラーになります。
com.sun.istack.internal.SAXException2: unable to marshal type "commerce.api.rest.resources.PromotionResource " as an element because it is not known to this context.
Accept ヘッダーが application/json または application/hal+json であれば問題ありません。この問題は、クライアント (この場合はクロム) が application/xml を探している場合にのみ発生します (HATEOAS がクライアントの要求に従っているため、これは理にかなっています。XML メッセージ コンバーターをリストに追加するスプリング ブートの @EnableAutoConfiguration を使用しています)。したがって、XML コンテンツ タイプを有効にします。
少なくとも 2 つのオプションがあると思います: 1. jaxb エラーを修正する 2. サポートされているコンテンツ タイプから xml を削除する
どちらかを行う方法がわからないか、別のオプションがあるかもしれません。