Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Restfull Jax-RS annotation support

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>
like image 338
Ahmet Karakaya Avatar asked Aug 28 '14 08:08

Ahmet Karakaya


People also ask

Does Spring support JAX-RS?

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.

Which annotation is used for creating a RESTful web service using JAX-RS?

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.

What is the difference between JAX-RS and spring REST?

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.

What are JAX-RS annotations?

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.


3 Answers

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.

Update

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.

like image 55
Maleen Abewardana Avatar answered Oct 28 '22 07:10

Maleen Abewardana


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

like image 26
PaulNUK Avatar answered Oct 28 '22 07:10

PaulNUK


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.

  1. Same relative paths in multiple @Controllers not supported
  2. @ExceptionHandler is controller-centric
  3. Standard content negotiation can’t respond with a fixed response type (SPR-6937)
  4. JSR 303 bean validation not applied in @Controllers (SPR-6928, scheduled for 3.1)
  5. Formatting responses (i.e. Date) not working using Spring formatter annotations
  6. You cannot return a bean from an exception handler and expect it to be automatically serialized to xml or json based on the incoming accept header.

HTH...

like image 28
Ripu Daman Avatar answered Oct 28 '22 08:10

Ripu Daman