0

I am learning Spring and trying make simple SOA project and I have simple test class:

@Path("/hello")
public class HelloWorldResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayhello() { return "hello"};
}

But I want interface for JAX-RS and some autoinjected implementation:

@Path("/hello")
public interface HelloWorldResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    String sayhello();
}

public class HelloWorldImpl implements HelloWorldResource {
    @Override
    public String sayhello() {
        return "hello";
    }
}

And I know that in Java EE I can do it with one annotation (@Stateless for example) But how can I do the same with Spring 4+?

bearhunterUA
  • 259
  • 5
  • 15
  • It is not about Spring. [answer][1] [1]: http://stackoverflow.com/questions/16950873/is-it-possibile-to-define-a-jax-rs-service-interface-separated-from-its-implemen – bearhunterUA Aug 05 '15 at 19:49

1 Answers1

1

This post makes a few assumptions:

  1. I assume you have set up resource discovery, IE your servlet is correctly routing /hello to the HelloWorldResource.
    If this doesn't make any sense to you, you'll want to look into registering Jax-RS resources with your servlet container.
  2. I assume you have set up a Spring context by registering a org.springframework.web.context.ContextLoaderListener in your web.xml.

You will need to mark your HelloWorldImpl to be discovered by spring. The easiest way to do that is to enable annotation-processing in your spring context.xml (loaded by passing it as contextConfigLocation to your ContextLoaderListener).

Then it should be sufficient to mark HelloWorldImpl with a @Controller and declare it as a bean in your context.xml.

Vogel612
  • 5,620
  • 5
  • 48
  • 73