I have a Spring Boot application that uses springdoc-openapi-ui to document REST endpoint. Also, there is some simple UI with Spring MVC. For some reason, in Swagger UI I see not only schemas for REST but also the schema for ModelAndView class. Is there a way to remove it from there?
I've tried already some options like limiting packages to scan with springdoc.packagesToScan or springdoc.model-and-view-allowed but without any results?
You can hide a Controller or Schema classes with #Hidden annotation, like this:
import io.swagger.v3.oas.annotations.Hidden;
#RestController
#Hidden
public class ItemController
#Hidden annotation is part of springdoc-openapi-ui library.
Related
I am trying to make a simple Spring Boot Application with the simple controller (Restful controller) where the function is returning the String that is to be printed on the Web page. But I am facing a problem where I am getting 404 error every time. In my views, I am setting up the application right. I have just used spring-boot-starter-web as the dependency.
Application file:
Any help would be appreciated.
And I know #SpringBootApplication automatically take all the annotation.
Either your controller class needs to be in or sub package of SpringBoot Application class or the package in which controller class exists needs to be scanned explicitly using #ComponentScan to resolve this
Can you show me the structure of your project?, I believe the problem Spring is not finding your userController, #SpringBooApplication use the #ComponentScan how is in charge to search for all the spring annotations un your code, so I believe is something
Need to add #ComponenScan the resquest to http:localhost:8080/api should return "Hello" if you need to add a path http:localhost:8080/user/api do as follow:
#RestController
#RequestMapping(value = "/user")
I'm trying to hide a REST endpoint based on runtime configuration in Spring and Jersey. The most straightforward way is to throw the NotFoundException from the controller itself but maybe there's more kosher. The controller is registered in the constructor of the config class which extends org.glassfish.jersey.server.ResourceConfig.
I thought of using the #Profile annotation on the controller but I can still access the endpoint. When I hit that endpoint, I get the following error:
o.g.j.s.s.SpringComponentProvider - None or multiple beans found in Spring context
but then Jersey manages to access that controller, which I confirmed by attaching a debugger to the Spring process. So Jersey does not honor the #Profile setting.
On a separate note, I also have Swagger plugged into Jersey and when accessing the definition endpoint (.../swagger.json) I can see the endpoint provided by the #Profile-disabled controller.
Is there anything better I can do here is is throwing the NotFoundException the best option?
Note: Sorry, I thought I saw that you were using Spring Boot. The following answer is only relevant for Spring Boot.
#Profile is only good for Spring bean registration, but you are still registering the service with Jersey as a resource. What you can do is use a ResourceConfigCustomizer and add the #Profile to the customizer. This way it will only register the resource with Jersey ResourceConfig if the correct profile is active.
#Component
#Profile("..")
public class MyResourceConfigCustomizer implements ResourceConfigCustomizer {
#Override
public void customize(ResourceConfig config) {
config.register(MyResource.class);
}
}
In a latest video about Kotlin and Spring Boot: Spring Tips: Bootiful Kotlin Redux. The Application class of Spring Boot looks like:
class SpringBootKotlinApplication
fun main(args: Array<String>) {
}
I remember a Kotlin has to be annotated as open in Spring Boot.
open class SpringBootKotlinApplication
See this video.
So why it is not necessary to be annotated open now? Spring Boot does need to extend the Application class now?
When you have a look at one of the speaker’s sample projects like this, you will notice a compiler plugin, in this case kotlin-maven-allopen. It’s described in the official docs:
For instance, when you use Spring, you don't need all the classes to be open, but only classes annotated with specific annotations like #Configuration or #Service. All-open allows to specify such annotations.
As SpringBootAnnotation, as a meta annotation, is fulfilling that condition, there’s no need to make the class open in the source code since the compiler does it behind the scenes.
The point is, how exactly to identify a Spring MVC implementing RESTful web services? Assuming use of Spring 3.x, use of which all annotations in the project will indicate that the project implements RESTful web services?
If the controller is annotated with #RestController or the #RequestMapping methods has #ResponseBody on return type then those are REST Service.
Assuming you are using Spring 3.x
All the Spring MVC components has to use the common #Controller annotation to mark that as the controller servlet.
In short controller servlet should be annotated with #Controller
When you implement a RESTful web services in Spring 3.x, the response would be always sent with the response body.
In short Controllers which implement a REST API should be annotated with #Controller+#ResponseBody
Additional information
Spring 4.0 has provided a specialized version of controller- #RestController.
#RestController is a stereotype annotation that combines #ResponseBody and #Controller. #RestController annotation itself annotated with #Controller and #ResponseBody.
#Target(value=TYPE)
#Retention(value=RUNTIME)
#Documented
#Controller
#ResponseBody
public #interface RestController
See the Spring docs for more information
How does SimpleCORSFilter work in this example?
Enabling Cross Origin Requests for a RESTful Web Service.
I only see a declaration of SimpleCORSFilter class but no instance. I tried ctrl+f to search the example page but can't find anywhere this class be instantiated.
How does it work?
I am new to Spring and Java.
So more detail more helpful. Thx.
A main point of Spring is a mechanism called dependency injection. Spring allows you to mark your classes, instance variables and so on with special annotations. Spring will look for those annotations and configure your application according to them.
In your example you annotate your filter with #Component:
#Component
public class SimpleCORSFilter implements Filter
And you annotate your Application class with #SpringBootApplication:
#SpringBootApplication
public class Application
The second annotation (#SpringBootApplication) tells Spring to search through your project for #Component annotations. As you annotated your filter with this, Spring will find your filter and instantiate it automatically. That's how your filter will be created and put to the right place.