[英]How to get Springfox to serialize jaxb annotated models when generating Swagger documentation
我一直在嘗試使用Springfox將我的spring-boot API記錄為Swagger文檔,但是我無法弄清楚如何使用它來序列化我的jaxb注釋模型(它們來自maven依賴,因此是只讀的) 。 我在這里做了很多搜索,而Springfox github頁面和文檔似乎無法弄明白。 我也在我的控制器上嘗試了許多不同的swagger注釋,但大多數時候我找到了一個未找到類的異常,因為它找不到類ApiImplicitParam,我甚至沒有使用它。 任何幫助,將不勝感激。
這是我的處理程序:
@RequestMapping(
value = "/GnAcctDetailsInq",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.POST
)
public ResponseEntity<GnAcctDetailsInqResponse> gnAcctDetailsInq(
@RequestHeader HttpHeaders requestHeaders, @RequestBody GnAcctDetailsInqRequest request)
throws ClassNotFoundException, XmlMappingException, IOException, MessengerException,
AuthException {
log.info("Sending request message: {}", request);
String xmlResponse = autoapiService.sendAndReceive(requestHeaders, request);
GnAcctDetailsInqResponse domainObjectResponse = (GnAcctDetailsInqResponse) autoapiService.convert(xmlResponse);
HttpHeaders responseHeaders = autoapiService.createResponseHeaders(domainObjectResponse);
return new ResponseEntity<GnAcctDetailsInqResponse>(domainObjectResponse, responseHeaders, HttpStatus.ACCEPTED);
}
我的Springfox Swagger配置:
@Bean
public Docket autoapi() {
List<ResolvedType> otherModels = getNonGnModels();
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build()
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/autoapi";
}
})
.genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(
newRule(typeResolver.resolve(DeferredResult.class,
typeResolver.resolve(ResponseEntity.class, WildcardType.class)),
typeResolver.resolve(WildcardType.class)))
// This method expects the first argument to be only one instance of a ResolvedType, the second one can be an array.
.additionalModels(typeResolver.resolve(Error.class)
, otherModels.toArray(new ResolvedType[otherModels.size()]));
}
而且我相當確定我配置了對象映射器,因為它應該也是如此:
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
@Primary
public ObjectMapper objectMapper() {
return new ObjectMapper()
.registerModules(new JaxbAnnotationModule())
.setSerializationInclusion(Include.NON_NULL)
.enable(SerializationFeature.INDENT_OUTPUT)
.enable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)
.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
.enable(JsonParser.Feature.ALLOW_MISSING_VALUES);
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> messageConverters) {
messageConverters.add(new MappingJackson2HttpMessageConverter(objectMapper()));
}
@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper objectMapper = null;
for (HttpMessageConverter<?> converter : converters) {
if (converter instanceof MappingJackson2HttpMessageConverter) {
MappingJackson2HttpMessageConverter jacksonConverter =
((MappingJackson2HttpMessageConverter) converter);
if (objectMapper == null) {
objectMapper = jacksonConverter.getObjectMapper();
} else {
jacksonConverter.setObjectMapper(objectMapper);
}
}
}
}
}
此外,這是響應對象的模型看起來 - 它們都非常相似,有些具有引用我的另一個模型的字段,其他只是具有String和int字段的對象。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"accountInfo", "requestStatus"})
@XmlRootElement(name = "gnAcctDetailsInqResponse")
public class GnAcctDetailsInqResponse implements Serializable {
@XmlElement(name = "AccountInfo")
protected AccountInfo accountInfo;
@XmlElement(name = "RequestStatus", required = true)
protected RequestStatus requestStatus;
public AccountInfo getAccountInfo() {
return this.accountInfo;
}
public void setAccountInfo(AccountInfo value) {
this.accountInfo = value;
}
public RequestStatus getRequestStatus() {
return this.requestStatus;
}
public void setRequestStatus(RequestStatus value) {
this.requestStatus = value;
}
}
與其他模型一樣,此模型在Swagger文檔中表示為:
"GnAcctDetailsInqResponse": {
"type": "object",
"title": "GnAcctDetailsInqResponse",
"xml": {
"name": "GnAcctDetailsInqResponse",
"attribute": false,
"wrapped": false
}
我還通過改變一些按照我的意願序列化我的模型的jackson-module-jsonSchema代碼制作了一個json模式生成器,是否有可能以某種方式將這些模式定義注入到Swagger doc的定義對象中? 我對任何類型的解決方案持開放態度。
經過一番進一步的調查后,我發現只有序列化字段的類型才是字段沒有用@XmlElement
注釋的字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.