我一直在尝试使用Springfox将我的spring-boot API文档化为Swagger文档,但我无法弄清楚如何序列化我的jaxb注释模型(这些模型来自于maven依赖项,因此是只读的)。我在这里进行了大量搜索,查看了Springfox github页面和文档,但似乎仍然没有找到答案。我也尝试在我的控制器上使用各种swagger注释,但大多数情况下我收到一个class not found exception,因为它找不到ApiImplicitParam类,而我甚至没有使用该类。希望能得到任何帮助。
以下是我的处理程序:
我的Springfox Swagger配置:
此外,这是响应对象的模型,它们都非常相似,有些字段引用了我的另一个模型,其他则只是带有String和int字段的对象。
以下是我的处理程序:
@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文档的definitions对象中?我对任何类型的解决方案都持开放态度。
经过进一步调查,我注意到只有那些没有用@XmlElement
注释的字段才会被序列化。