我已经开始使用Springfox来记录XML REST API,这虽然很好,但是存在两个相关问题:
@XmlRootElement(name = "ChildrenResponse", namespace = DomainConstants.NAMESPACE) @XmlAccessorType(XmlAccessType.NONE) @XmlType(name = "ChildrenResponse") public class ChildrenResponse { @XmlElement(name = "Child") private List child = new ArrayList(); @XmlElement(name = "Paging", required = true) private Paging paging; @XmlElement(name = "AdditionalInformation", required = true) private AdditionalInformation additionalInformation; public ChildrenResponse() { } public ChildrenResponse(AdditionalInformation additionalInformation) { this.additionalInformation = additionalInformation; } public List getChild() { return child; } public void createPaging(String previous, String next, long size) { paging = new Paging(previous, next, size); } public Paging getPaging() { return paging; } public AdditionalInformation getAdditionalInformation() { return additionalInformation; } }
...以及由此定义的控制器方法:
@RequestMapping(value = "/{ref}/children", method = RequestMethod.GET) @ApiOperation(value = "Get children", notes = "Returns a paged list container with links to children of this SO", response = ChildrenResponse.class) @ApiResponses({ @ApiResponse(code = 200, message = "Success", response = ChildrenResponse.class), @ApiResponse(code = 404, message = "If the requested ref does not reference an accessible SO") }) @ResponseBody @SuppressWarnings("checkstyle:magicnumber") public String getChildren(@PathVariable("ref") String ref, HttpServletRequest request) throws JAXBException, ParserConfigurationException, BadInputException { ... }
controller方法的结果是来自JAXB的序列化字符串,该字符串将其正确格式化为例如:
<ChildrenResponse xmlns="(ns-uri)"> <Child title="Title">http://server/api/object/ref1</Child> <Paging> <TotalResults>1</TotalResults> </Paging> <AdditionalInformation> <Self>http://server/api/object/ref2/children</Self> </AdditionalInformation> </ChildrenResponse>
但是,在大多数情况下,Springfox的模型定义未遵循注释或属性顺序。认为模型是
"ChildrenResponse":{"type":"object", "properties":{ "additionalInformation":{"xml":{"name":"AdditionalInformation","attribute":false,"wrapped":false},"$ref":"#/definitions/AdditionalInformation"}, "child":{"type":"array","xml":{"name":"Child","attribute":false,"wrapped":false},"items":{"$ref":"#/definitions/ChildInfo"}}, "paging":{"xml":{"name":"Paging","attribute":false,"wrapped":false},"$ref":"#/definitions/Paging"}},"title":"ChildrenResponse"}
请注意小写字母开头的名称和不同的顺序。
我是否还必须使用@ApiProperty装饰每个属性?
我了解到您可以通过在Springfox的配置中手动添加JSON注释处理器来解决此问题,但我不知道该在何处进行。
此外,假设此端点从未使用过Paging属性。我想使用一个填充的ChildrenResponse对象构造文档中所示的示例,而不是让它填充带有'string'和'0'的字段,并且能够在该响应中包含空字段。
我正在将Springfox 2.9.2与Spring MVC(具有XML配置)4.3结合使用。