swagger-maven-plugin
swagger-maven-plugin copied to clipboard
"Empty" swagger.json File
I am using version 3.1.0 of the swagger-maven-plugin with the following configuration:
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>true</springmvc>
<locations>my.api.location</locations>
<schemes>http</schemes>
<host>myhost</host>
<basePath>/api</basePath>
<info>
<title>Title</title>
<version>v1</version>
<description>Description</description>
</info>
<swaggerDirectory>my/directory</swaggerDirectory>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
When I run mvn compile the swagger.json file generated contains only the basic information, IE the info, host, basepath, and schemes. It does not contain any information about my API.
I am using io.swagger v1.5.3
The annotations is similar to this
@API(value = "value", tags = {"tag"}, hidden = false)
When checking the debug logs I see the following message:
[INFO] Reflections took 136 ms to scan 1 urls, producing 38 keys and 349 values
I am able to compile the example project with the correct swagger.json so it shouldn't be an issue due to my maven version.
Am I missing something or could this be a bug.
Relates to this issue
I am getting the same issue with -
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.3</version>
me too, probably downgrade to 3.0-M2-SNAPSHOT should help.
Chiming in to say that I'm also having the exact same issue. I'm already using springfox for dynamic generation of the swagger-ui.html and such (it this works) it uses the io.swagger annotations as well.
But with both 3.1.3 and 3.0-M2-SNAPSHOT I'm getting an empty json as well. (We need a static version, which is why I'm trying to use the plugin) So far, I see nothing while in debug, no errors, nothing.
Can be related to https://github.com/kongchen/swagger-maven-plugin/issues/218?
Doubt it, all my @RequestMapping has values
+1
Are your annotated methods public?
I'm having the same issue.... My maven plugin looks similar to the above. My methods is public, and look like this:
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Gets a post"),
@ApiResponse(code = 401, message = "Something terrible happened", response=Error.class) })
@RequestMapping(value = "/post/{id}", method=RequestMethod.GET)
public @ResponseBody Post getPost(@ApiParam(value="ID number corresponding to a specific post") @PathVariable("id")int id)
Do you have a @RequestMapping annotation at class level?
@RequestMapping("") public class A{ @RequestMapping("/hello") public Post method(){ ... } }
If you do... can you post your class?
I didn't have that, but adding it didn't help:
@RestController
@RequestMapping(value = "")
@Api(value="/")
public class ExampleController {
All the methods inside have a similar signature as before.
@nwagg14 Could you please share a gist with your class source code ?
So my problem was that I didn't have a @ApiOperation on my methods. If anyone else is having this problem, ensure you have @RequestMapping on all methods AND your class, and that you have @ApiOperation for every method, not just @ApiResponse
@nwagg14 Can you share your original code and the fixed one using gist, please?
Adding @ApiOperation fixed my problem. I didn't need to add @RequestMapping element
@javitorres. If you annotate a method that is not exposed as REST endpoint (@RequestMapping from Spring), that will generate "fake" swagger documentation, isn't?
I had this problem and no matter how I altered my project (adding removing annotiations, changing swagger and swagger maven plugin versions) i could not get the file to generate with the content that it should have contained. However, when i switched to do "mvn install swagger:generate" everything worked perfectly. This is confusing because I've never had to specify "swagger:generate" on any of the previous microservices I have developed. I'm not sure what's different about my current project, but without that maven goal the file is always empty. So now, I'll always use "swagger:generate" in my maven goal. Hope this helps others.
Your plugin was declared in a pluginmanagement block maybe?
@gsugambit do you have a phase block to attach your plugin to your maven life cycle?
Can you share the code? As gist if possible
I was experiencing the same in a multi module maven setup. In my case what fixed it was to add the direct dependency from the module where the plugin is defined to the module containing the resources. It didn't pick it up through the transitive dependency. Don't know if this is a bug or the expected behavior. My 2 cents, hope it helps you.
Adding RequestMapping or Api annotations doesnt help. Here is my plugin configuration below:
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.1.7</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>false</springmvc>
<locations>tr.com.mkk.kfs.kfs.web</locations>
<basePath>/api</basePath>
<info>
<title>Kitle Fonlama Sistemi REST API</title>
<description>Add some description</description>
<version>v1</version>
</info>
<swaggerDirectory>${project.build.directory}/swagger</swaggerDirectory>
<attachSwaggerArtifact>true</attachSwaggerArtifact>
</apiSource>
</apiSources>
</configuration>
<executions>
<execution>
<phase>${phase.generate-documentation}</phase>
<!-- fx process-classes phase -->
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
On of my rest controllers:
` @Slf4j @RestController @RequestMapping("api/member/") @Api public class MemberController {
@Autowired
MemberService memberService;
/**
* @param saveMemberInfo
* @return
*/
@ApiOperation(
value = "The operation saves a new member.",
notes = "<a href=\"https://manavgat.mkk.com.tr/api/parameter/v1/listAll\" target=\"_blank\">Click to see updatable parameters</a>")
@RequestMapping(value = Constants.MEMBER_SAVE, method = RequestMethod.POST)
public ResponseEntity<Object> saveMember(@RequestBody SaveMemberInfo saveMemberInfo) {
memberService.saveMember(saveMemberInfo);
return ResponseEntity.status(HttpStatus.CREATED).body(ResponseBuilder.build(saveMemberInfo, HttpStatus.CREATED, Constants.SUCCESS));
}
} `
And the generated swagger.json file:
{ "swagger" : "2.0", "info" : { "description" : "Add some description", "version" : "v1", "title" : "Kitle Fonlama Sistemi REST API" }, "basePath" : "/api" }
@jivimberg comment solved the issue for me, huge kudos 👏