Is #RequestMapping annotation a replacement of DWR? - spring-boot

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.

Related

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.

JerseyConfig and #Profile to hide a REST endpoint

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);
}
}

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 MVC with RESTful implementation and simple Spring MVC, how to differentiate between them?

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

Invoking custom annotation using Spring AOP

I searched the web for a clear example on how to invoke my custom method annotation using Spring AOP but couldn't find a clear example.
I am building a framework to inject a user profile in the context when certain methods on any POJO is called.
The framework API should be invoked via a custom method annotation, say #RunAsAdmin. I can build the annotation part and parser, my question is what is the best way to invoke my parser when the annotated method is called.
We are using Spring 3.0 and would like to know what is the best practice to configure Spring framework to understand those methods annotated with specific annotation and invoke my annotation handler (parser).
Any example or guidance will be highly appreciated.
Intercepting a call on any annotated Spring bean method is straightforward, see the example below.
Intercepting a call on a POJO is not possible by definition - you have to return a proxy instead.
That is, you should implement a factory for these POJOs that will return proxies instead of real POJOs. You might use ProxyFactoryBean http://docs.spring.io/autorepo/docs/spring-framework/3.2.0.BUILD-SNAPSHOT/api/org/springframework/aop/framework/ProxyFactoryBean.html for this.
//intercepting a call to any method annotated with #com.yours.RunAsAdmin of any Spring-managed bean
#Component #Aspect class RunAsAdminAspect {
#Autowired
private YourRunAsAdminHandler handler;
#Before("#annotation(com.yours.RunAsAdmin)")
public void doRunAsAdmin() {
handler.grantAdminRightsToCurrentUser(); //your real stuff
}
}

Resources