views:

1057

answers:

1

I'm trying to start Jersey on a preconfigured port/url with a preconfigured resource instance. I can't quite figure out how to correctly do it.

Here is a snippet of a code. Help me, please, fill in the blanks:

@Component
@PerRequest
@Path("/svc")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class MyService
{
    // This piece is known
}

public class JSONMessageBodyWriter implements MessageBodyWriter<Object>
{
    // This piece is known
}

public class XMLMessageBodyWriter implements MessageBodyWriter<Object>
{
    // This piece is known
}

// This is where I need help
MyService service = new MyService();
...
HttpHandler handler = ???
...
HttpServer server = ???
server.createContext("/services", handler);
...
server.start();

In the snippet above, I'm trying to expose the MyService via the http://localhost:8080/services/svc url. If the JSONMessageBodyWriter and the XMLMessageBodyWriter will be plugged in - the service will work vis XML and JSON accordingly.

If you know how to do this on Jetty or Grizzly, let me know too. Can Spring help here?

+1  A: 

Jersey itself provides an entire set of examples, and specifically the simplest helloworld example shows how to start a server on a port to either just run it or test against in JUnits. If you look at that, you'll get example for how to setup and start a server.

Now, on configuring MessageBodyReaders and MessageBodyWriters as part of a jersey application, you'll find that this is covered by the JAX-RS spec itself (which jersey implements). First off, your reader and writer need the @Provider annotation. Additionally, the reader should get the @Consumes annotation, and the writer should get the @Produces annotation, so you can specify what mime-type(s) they consume and produce, respectively.

Next is activating them. The helloworld example above won't show that, because it doesn't use custom readers or writers (another example might, I didn't look). So instead of supplying the package to find resources (as they do; you'll know what I speak of when you see the helloworld example), you'll code a subclass of Application, where you specify your resource class and the reader/writer classes. With the reader and writer you have the option of specifying either a class (that you return from getClasses), or providing an already created instance yourself (that you return from getSingletons).

Finally, specify the name of your Application sub-class as the value of init-parameter "javax.ws.rs.Application". The init-params can be passed to GrizzlyWebContainerFactory.create (again, you'll see this used in the example) when you start the server.

Hope this helps.

StevenC
Apparently new users can only use one link, or I else I would have had about 6 more links. Sorry about that!
StevenC
The annotations and Application class can be found at https://jsr311.dev.java.net/nonav/releases/1.0/index.html
StevenC