返回 Resources<T> 时 Spring HATEOAS (w Spring Boot) JAXB 编组错误或 PagedResources<T>;结果 [英] Spring HATEOAS (w Spring Boot) JAXB marshal error when returning a Resources<T> or PagedResources<T> result
问题描述
我的控制器中有这样的东西:
I've got something like this in my controller:
@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 错误,其根本原因是:
If i navigate to the URL mapped to that controller method i get a 500 error with a root cause of:
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 注释,它就会变成这个错误:
If i throw a @XmlRootElement annotation on my resource it becomes this error:
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.
如果接受头是 application/json 或 application/hal+json,一切都很好.仅当客户端(在本例中为 chrome)正在查找 application/xml 时才会导致问题(这很有意义,因为 HATEOAS 正在遵循客户端请求.我正在使用 Spring Boot 的 @EnableAutoConfiguration,它将 XML 消息转换器添加到列表中从而启用 XML 内容类型.
Everything is fine if the accept header is application/json or application/hal+json. The problem is caused only when the client (in this case chrome) is looking for application/xml (which makes sense as HATEOAS is following the clients requests. I'm using spring boot's @EnableAutoConfiguration which is adding the XML message converter to the list and thus enabling XML content types.
我猜我至少有两个选择:1.修复jaxb错误2.删除xml作为支持的内容类型
I'm guessing i have at least 2 options: 1. fix the jaxb error 2. remove xml as a supported content type
不知道该怎么做,或者有其他选择.
not sure how to do either, or maybe there's another option.
推荐答案
如果您实际上不想生成 XML,请尝试使用 @RequestMapping
的 produces
属性注释.类似:@RequestMapping(produces=MediaType.APPLICATION_JSON_VALUE)
If you don't actually want to produce XML try using the produces
attribute of the @RequestMapping
annotation. Something like: @RequestMapping(produces=MediaType.APPLICATION_JSON_VALUE)
或者,您可以从类路径中排除 jaxb
或查看添加自己的 org.springframework.boot.autoconfigure.web.HttpMessageConverters
bean 以完全控制已注册的HttpMessageConverter
的.查看 WebMvcConfigurationSupport.addDefaultHttpMessageConverters
以查看 Spring 默认添加的内容.
Alternatively you could exclude jaxb
from you classpath or look at adding your own org.springframework.boot.autoconfigure.web.HttpMessageConverters
bean to take complete control of the registered HttpMessageConverter
's. See WebMvcConfigurationSupport.addDefaultHttpMessageConverters
to see what Spring will add by default.
这篇关于返回 Resources<T> 时 Spring HATEOAS (w Spring Boot) JAXB 编组错误或 PagedResources<T>;结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!