17

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>
Vogel612
  • 5,620
  • 5
  • 48
  • 73
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141

3 Answers3

11

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.

Community
  • 1
  • 1
Maleen Abewardana
  • 13,600
  • 4
  • 36
  • 39
  • 1
    +1 Good answer! Spring also integrates with Jersey which is the reference implementation of JAX-RS – geoand Aug 28 '14 at 08:36
  • 1
    ın that case I think @PATH annotation is handled by Jersey API – Ahmet Karakaya Aug 28 '14 at 08:37
  • 1
    Yes it does. But as with RestEasy integration, you can inject Spring services into Jersey controllers. As Maleenc pointed out, there is no Spring implementation of JAX-RS. The best you can do is integration a JAX-RS implementation (such as Jersey or RestEasy) with Spring to provide a Spring "backend" to the JAX-RS frontend – geoand Aug 28 '14 at 08:45
  • Sorry this is wrong - you can use javax,ws annotations in Spring. See my answer below. The Spring boot example is using multiple javax.ws annotations. – PaulNUK Jan 11 '17 at 09:39
7

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

PaulNUK
  • 4,774
  • 2
  • 30
  • 58
  • You need spring-boot-starter-jersey dependency in the classpath. http://docs.spring.io/spring-boot/docs/1.2.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-jersey – Maleen Abewardana Jan 11 '17 at 16:02
3

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...

Ripu Daman
  • 2,482
  • 2
  • 18
  • 22