我一直在尝试使用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
答案 0 :(得分:1)