Chr*_*our 5 spring-hateoas spring-boot
我的控制器中有以下内容:
@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);
}
Run Code Online (Sandbox Code Playgroud)
如果我导航到映射到该控制器方法的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
Run Code Online (Sandbox Code Playgroud)
如果我在资源上抛出@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.
Run Code Online (Sandbox Code Playgroud)
如果accept标头是application / json或application / hal + json,那么一切都很好。仅当客户端(在本例中为chrome)正在寻找application / xml时才引起问题(这在HATEOAS遵循客户端请求时才有意义。我使用的是Spring Boot的@EnableAutoConfiguration,它将XML消息转换器添加到列表中从而启用XML内容类型。
我猜我至少有2个选择:1.修复jaxb错误2.删除xml作为受支持的内容类型
不知道该怎么做,或者还有另一种选择。
如果您实际上不想生成 XML,请尝试使用注释produces的属性@RequestMapping。就像是:@RequestMapping(produces=MediaType.APPLICATION_JSON_VALUE)
或者,您可以jaxb从类路径中排除或添加您自己的org.springframework.boot.autoconfigure.web.HttpMessageConvertersbean 以完全控制注册的HttpMessageConverter。看看WebMvcConfigurationSupport.addDefaultHttpMessageConvertersSpring 默认会添加什么。
| 归档时间: |
|
| 查看次数: |
3479 次 |
| 最近记录: |