Spring MVC with RESTful implementation and simple Spring MVC, how to differentiate between them? - spring

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

Related

Is #RequestMapping annotation a replacement of DWR?

I'm new to the spring-boot framework and a little bit confused.
Previously we use DWR to convert JAVA method in server's war package to javascript function. In Spring boot is the annotation #RequestMapping do the same? Thx.
No, the #RequestMapping annotation is not related to Direct Web Remoting. The annotation is used in the controller layer of Spring web applications to map requests onto methods.
A typical example for a RequestMapping looks as follows:
#RequestMapping(method = RequestMethod.GET, path = "/users")
public ResponseEntity<List<Post>> getPosts() {
[...]
}
This will map incoming GET requests to path /users to this method.

Remove ModelAndView from Swagger 3.0 UI

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.

Can we interchange #Controller and #Service in spring?

Can we interchange #Controller and #Service in spring? I tried and it was working. How are they implemented internally?
#Controller and #Service are special form of #Component. Spring allows you to use it interchangeably, but is it NOT recommended to do it that way. As for instance the #Controller is used on classes to that serves are the Controller on the MVC. Also, spring dispatcher servlet will scan for #RequestMapping on classes which are annotated using #Controller.
#Component
------#Controller
------#Service
------#Repository
#Controller and #Service are ultimately part of #Component. You may interchange them but it is not recommended or follow best practices. The purpose of using 3 different annotation is to we can have separate the layers based on it use more appropriate annotation.
#Controller:
Annotated class indicates that it is a controller component, and mainly used at the presentation layer.
#Controller is used to mark classes as Spring MVC Controller. This annotation is just a specialized version of #Component and it allows the controller classes to be auto-detected based on classpath scanning.
#Service:
It indicates annotated class is a Service component in the business layer.

Can we use org.springframework.ws.server.endpoint.annotation.Endpoint annotated class to create RESTful web service with Spring

I have to create RESTful web service in an existing application which currently provides SOAP services and thus uses org.springframework.ws.server.endpoint.annotation.Endpoint, so could I use #Endpoint annotated class instead of #RestController to create RESTful web service with Spring and can these classes have following method:
#RequestMapping(value = "/myMethod", method=RequestMethod.GET)
public ResponseEntity<String> restMethod() {
...
}
#Endpoint annotation mark a class as a SOAP endpoint, which will handle through its methods SOAP requests. Whereas #RestController, according to it javadoc, is "a convenience annotation that is itself annotated with #Controller and #ResponseBody", that is a very convenient mechanism to create REST services. So you can not use #Endpoint waiting for it to behave like #RestController.

Spring boot jersey - prevent startup instantiation of controller

I am using spring boot with web and jersey (spring-boot-jersey-starter). I have a Jersey endpoint that needs to inject a request scope bean. However, at startup of the application I am getting a no bean found error.
#Component
#Path("blah")
#RequestScoped
public class JerseyController{
#Inject
private MyEntity entity;
}
#Component
public class JerseyConfiguration extends ResourceConfig{
public JeyseyConfiguration(){
register(JeyseyController.class);
registere(MyEntityProvider.class);
}
}
Is there a way, in a spring-boot web app, to prevent Spring from attempting to instantiate and inject my JerseyController until an HTTP request is received so that the injected dependency can be provided by my Jersey provider?
#Component is not required on Jersey resources. Having it will cause Spring to instantiate it (with default Singleton scope). I don't think Spring doesn't respect the #RequestScoped. This is a Jersey annotation. If you want to use the #Component, I think the Spring #Scope("request") might do the trick though.
You can also remove the #RequestScoped. This is the default scope for Jersey resources.
The only time I have ever found a need to use #Component on Jersey resources, is if I need to use the Spring #Value (maybe AOP also, but I don't do much AOP). Other than that, the Jersey-Spring integration already supports the most common used feature of Spring which is DI. And if you really want to make the Jersey resource a singleton, Jersey supports the #Singleton annotation.

Resources