简体   繁体   中英

How to get Springfox to serialize jaxb annotated models when generating Swagger documentation

I've been trying to document my spring-boot API as a Swagger document using Springfox, but I can't figure out how to get it to serialize my jaxb annotated models (which are from a maven dependency, so are read-only). I've done a whole lot of searching on here, and the Springfox github page and documentation and can't seem to figure it out. I've tried a host of different swagger annotations on my controller as well, but mostly I got a class not found exception because it couldn't find a class ApiImplicitParam, which I didn't even use. Any help would be appreciated.

Here is my handler:

  @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);
  }

My Springfox Swagger configuration:

@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()]));       
}

And I'm fairly certain I configured the object mapper as it is supposed to be as well:

@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);
                }
            }
        }
    }
}

Also, this is how the model for the response Object looks -- they are all pretty similar, some have fields where they refer to another one of my models, others are just objects with String and int field.

@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;
    }
}

This model, like others is represented in the Swagger doc as:

"GnAcctDetailsInqResponse": {
  "type": "object",
  "title": "GnAcctDetailsInqResponse",
  "xml": {
    "name": "GnAcctDetailsInqResponse",
    "attribute": false,
    "wrapped": false
  }

I have also made a json schema generator by altering some of the jackson-module-jsonSchema code that serializes my models as I wish, would it be possible to somehow inject those schema definitions inside the definitions object of the Swagger doc? I'm open to any type of solution.

After a little further investigation I noticed the only types that have their fields serialized are ones where the fields aren't annotated with @XmlElement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM