如何将Swagger生成的JSON转为Java类?求Eclipse兼容工具及Grails集成方案
Absolutely! There are several solid tools and plugins that can automate converting Swagger UI's JSON to Java classes, and most integrate seamlessly with Eclipse and the Groovy Grails framework. Let me break down the most practical options to save you from rewriting code every time your Swagger JSON changes:
1. Swagger Codegen (Official & Versatile)
This is the go-to official tool for all things Swagger-related, and it’s perfect for your use case:
- It generates clean Java POJOs (and even Grails-friendly Groovy classes with optional annotations) directly from Swagger JSON/YAML.
- Eclipse Integration: Install the Swagger Codegen Eclipse plugin from the Eclipse Marketplace, or use its Maven/Gradle plugin in your Grails project. For Gradle, add the plugin to your
build.gradlefile, then run thegenerateSwaggerCodetask whenever your Swagger JSON updates—classes will regenerate automatically. - Quick command-line example (if you prefer that):
Just import the generated classes into your Eclipse/Grails project, and you’re set.swagger-codegen generate -i http://your-swagger-endpoint/swagger.json -l java -o ./generated-classes
2. Eclipse JAXB Tools (Built-in, No Extra Installs)
If you prefer staying within Eclipse’s native tools, JAXB can work with Swagger JSON with a small step:
- First, convert your Swagger JSON to an XSD schema (Swagger has built-in tools for this, or you can use a simple online converter).
- In Eclipse, right-click your project →
New→Other→JAXB→JAXB Classes from Schema. Select your XSD file, set your target package, and Eclipse will generate fully compatible Java classes. These classes work flawlessly in Grails projects, as Groovy is fully interoperable with Java.
3. Grails Swagger Plugin (Framework-Specific)
For a Grails-native solution, this plugin does double duty—it can generate Swagger docs and reverse-engineer Swagger JSON into Grails domain classes or DTOs:
- Install the plugin in your Grails project via
build.gradle, then configure the path to your Swagger JSON file. - Eclipse Integration: Use the Grails view in Eclipse to run the
grails generate-swagger-classescommand. Generated Groovy/Java classes will auto-populate in your project, and re-running the command after Swagger updates will refresh them instantly.
4. JSON Schema to Java (JS2P) Eclipse Plugin
Since Swagger JSON includes a JSON Schema definition, this plugin can directly convert that schema to Java classes:
- Install the JS2P plugin from the Eclipse Marketplace.
- Right-click your Swagger JSON file in Eclipse →
Generate Java Classes. Configure your package name, class naming conventions, and it will produce POJOs that play nicely with Groovy (including simplified getter/setter syntax that Grails loves).
Quick Pro Tips
- Always keep generated classes in a dedicated directory (like
src/generated/javaorsrc/generated/groovy)—this way, you never accidentally overwrite custom code, and regenerating is risk-free. - In Grails, use these generated classes as DTOs for request/response handling to cut down on manual boilerplate.
内容的提问来源于stack exchange,提问作者mGm

