Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support REST Controllers with default empty @RequestMapping (without any @RequestMethod) #378

Closed
neodem opened this issue Jan 26, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@neodem
Copy link

neodem commented Jan 26, 2020

I'm having trouble getting this to work. I've started with a working SpringBoot (v2.2.2) app with a working @RestController. To get springdoc to work I've included the dependency in my pom, built and started my service. I've then browsed to the api-docs endpoint and I get :
{"openapi":"3.0.1","info":{"title":"OpenAPI definition","version":"v0"},"servers":[{"url":"http://localhost:8081","description":"Generated server url"}],"paths":{},"components":{}}

Am I missing a step? I was assuming at runtime it would scan my code, find the @RestController, find the @RequestMapping methods and generate the api from that.

As a second attempt, I decided to annotate one of my methods with swagger @Operation annotations and still things don't get picked up.

Lastly, I tried specifying the package with my Controller manually using springdoc.packagesToScan and that didn't work either.

This feels like springdoc isn't seeing my Controller.. even though spring does.. it's perfectly active and it works..

Not sure what I'm missing..

@bnasslahsen
Copy link
Contributor

bnasslahsen commented Jan 26, 2020

Hi @neodem,

By default, you just need to add the springdoc-ui dependency, and you should see all the API when browsing to /v3/api-docs

<dependency>
  <groupId>org.springdoc</groupId>
  <artifactId>springdoc-openapi-ui</artifactId>
  <version>1.2.28</version>
</dependency>

You can have a look a the demos samples:

Please read the section of Using GitHub Issues:

If you are still facing this problem, please add the sample code or your git repository link to reproduce and this issue will be reopened.

@neodem
Copy link
Author

neodem commented Jan 26, 2020

I have posted on stack overflow with no luck, so I decided to dig into this myself. After a few hours of debugging I figured out what is going on in my situation.

when defining a REST Controller, spring allows you to declare @RequestMapping(value = "/init") with no explicit Method and will accept a POST request. However, in your OpenApiResource class you make a call to calculatePath, in there (on line 129) you make a call to the spring RequestMappingInfo.getMethodsCondition() and in my case you get back no results (since the method wasn't explicitly defined in the annotation).. thus no request method => no path calculated.

If I explicitly define the method: @RequestMapping(value = "/init", method = RequestMethod.POST) everything works as expected.

I think that at the least you should mention this in your documentation: "when defining @RequestMapping annotations you must declare a method and not rely on Springs default because springdoc won't find it).

I think a better solution would be to come up with a sensible default when nothing is explicity declared in the annotation.

@bnasslahsen bnasslahsen changed the title Package Scanning issue? Support REST Controllers with default empty @RequestMapping (without any @RequestMethod) Jan 27, 2020
@bnasslahsen bnasslahsen reopened this Jan 27, 2020
@bnasslahsen
Copy link
Contributor

Your description is now more accurate about the reason why the controller was not displaying.

The method value of @RequestMapping defaults to an empty array
We will add on the documentation that people should always be explicit, to avoid possibly nondeterministic behavior if multiple annotations can apply to the same request URL.

Note that by default, all the following methods are accepted by Spring 5: GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS.

@maratondiadelvidriero
Copy link

I had the same issue and solved by replacing @controller by @RestController. It will be very good to indicate this in the documentation.

@bnasslahsen
Copy link
Contributor

@maratondiadelvidriero,

You can have a look here:

@bnasslahsen bnasslahsen added the enhancement New feature or request label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants