【发布时间】:2018-03-20 14:31:46
【问题描述】:
我一直在尝试使用 Springfox 将我的 spring-boot API 记录为 Swagger 文档,但我不知道如何让它序列化我的 jaxb 注释模型(来自 maven 依赖项,因此请阅读-只要)。我在这里做了很多搜索,以及 Springfox github 页面和文档,但似乎无法弄清楚。我也在我的控制器上尝试了许多不同的招摇注解,但大多数情况下我得到了一个找不到类的异常,因为它找不到我什至没有使用的类 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 文档的定义对象中?我愿意接受任何类型的解决方案。
经过进一步调查,我注意到唯一将其字段序列化的类型是字段未使用 @XmlElement 注释的类型
【问题讨论】:
标签: java spring-boot swagger springfox