Can we use org.springframework.ws.server.endpoint.annotation.Endpoint annotated class to create RESTful web service with Spring - 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.

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.

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

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.

How #Service class is exposing and intercept methods on top of Repository

My question is in Spring Boot. I have created domain class, CRUD repository class and #Service wired service class. I have written some methods in #Service class like save entity based on some conditions and code is working fine.
My question is, how spring boot invokes particular method when I call REST POST method?

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

Resources