swagger codegen add @JacksonXmlElementWrapper(useWrapping = false) annotation to a single field

不羁的心 提交于 2020-03-26 03:21:28

问题


I'm currently using swagger codegen using yml to generate my models However I have one field, that is a List<Object> that needs to have the @JacksonXmlElementWrapper(useWrapping = false). I can see the @JacksonXmlElementWrapper in POJO.mustache but not in model.mustache. Does anyone know what to add in the yaml file or anywhere else so that field gets generated with that annotation? Thanks. I'm using spring-java language with gradle. I need this to be generated during build. so minimal changes are preferred.


回答1:


According to the readme on their git, Swagger Codegen mention ways to do this:

https://github.com/swagger-api/swagger-codegen/blob/master/README.md

since you're using gradle:

We can use a custom template for the code generation as follows:

// build.gradle
swaggerSources {
  inputFile = file('{name of your file}.yaml')
  xyz {
    language = 'spring'
    // template directory path
    templateDir = file('templates/{name of your custom directory}')
  }
}

inside templates/{name of your custom directory}, you can store your custom mustache files, as in your case, all you have to do is download the required spring templates from git (e.g. this link is for pojo.mustache) and add in the required changes to the template, along with the project and libraries folder. Run gradle build and it should generate.

There is a much simpler workaround however, where you can just use your own pojo class and add this in your existing config.json and remove the earlier entry for that model in your yaml file (of course):

  "importMappings" : {
      "{replace with className}": "{replace with packageName}.{replace with className}"
  }

Hope this helps.



来源:https://stackoverflow.com/questions/53368969/swagger-codegen-add-jacksonxmlelementwrapperusewrapping-false-annotation-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!