I have following class and web.xml files. Does spring framework supports jax-rs annotation such as @PATH
, and @PUT
, @Consumes
...etc.
In other word can I use @PATH
instead of @RequestMapping
Java:
import org.springframework.stereotype.Controller;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
@Controller
@Path("/register")
public class RegisterServices {
@PUT
@Path("/create")
@Consumes(MediaType.APPLICATION_JSON)
@Produces({"application/json"})
public Response create(@Context HttpServletRequest requestContex,
@HeaderParam("Authorization") String authorization,
String xMsisdn, String param) {}
}
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
There are currently 4 JAX-RS implementations available, all of which provide support for using Spring. Jersey is the reference implementation and the one used in this article.
Developing RESTful Web Services with JAX-RS. JAX-RS is a Java programming language API designed to make it easy to develop applications that use the REST architecture. The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services.
JAX-RS is only a specification and it needs a compatible implementation to be used. On the other hand, Spring MVC is a complete framework with REST capabilities. Like JAX-RS, it also provides us with useful annotations to abstract from low-level details.
Overview of JAX-RS Annotations Annotations in the JAX-RS API are used to provide meta-data around the web resource. A typical example is to use the @GET annotation with the @Path annotation to identify the method that should handle a GET request to the specified URI in the @Path annotation.
No, you cannot use javax.ws.*
annotations in spring. You can use something like resteasy
with spring. It is rather easy. If you need I can provide with an example. (Jersey and CXF has good JAX-RS implementations too.)
AFAIK Springsource has no idea to provide an implementation to JAX-RS. So if you want to use the features described in JAX-RS you will not get it directly from spring. But you can develop a rest web service using spring. That's a different story. A question was found on SO on that.
Depending on PaulNUK's answer below, I need to clarify my answer. End of the day rest is a specification, and someone needs to implement it in first place.
Question clearly asks, whether we can replace the annotations, you cant do it, unless you add an external dependency like Jersey to your classpath. That case implementation is provided by Jersey.
Therefore you would never be able to use spring implemented JAX-RS annotation ever.
Just put your JAX-RS (I'm using Jersey 2) annotations on a class, annotate that class with @Component to make it a Spring bean, and you have full JAX-RS support with Spring dependency injection.
So Spring hasn't reinvented the wheel by implementing JAX-RS itself, but integrates very easily with Jersey for example.
Here's a simple Spring boot example:
http://spring.io/blog/2014/11/23/bootiful-java-ee-support-in-spring-boot-1-2
If you want to develop the webservices using only Spring framework then Spring provides Spring MVC. Spring MVC has its own set of annotations. For e.g. "@RequestMapping". Spring MVC doesn't even adhere to the JAX-RS principles.
There are various opensource frameworks (like Jersey) which supports "JAX-RS" and can be integrated with Spring.
However, just in case you want to compare the Spring MVC with Jersey then below is the comparison. I personally support Jersey over SPring MVC as Spring MVC is not originally meant for webservices but for UI application.
HTH...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With