Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'd like to add a few things here.

Jersey is an implementation of JAX-RS. It has 3 major pieces: jersey-core, jersey-client, jersey-server. (The name should be obvious what they are for).

If you're writing JAX-RS services, you can return a few different formats: XML, JSON, ATOM. All you need to do is to annotate the method with the following annotation:

  // will return XML or JSON depending on the request from the client.

  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) 
This is a big win if you need to support both.

  - Object to XML conversion is done by JAXB.
  - Object to JSON conversion is done by Jackson via JAXB.
Jersey is part of JavaEE 6 standard (part of your Application Server if it supports it).

What's lacking from JavaEE 6 is an MVC framework which is targeted for JavaEE 7.

Another key feature is JAX-WS (the plain old SOAP WebService). The nice thing about JavaEE 6 is that the minimum differences in the programming style between JAX-RS and JAX-WS.

JAX-RS operates according to resources (e.g.: give me all students, give me student with id=1, delete student with id=1, etc). So some of the examples would be:

  // Rough pseudo-code, omitting a few JAX-RS annotation

  public class StudentResource{

  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

  public <List> all(){}

  @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })

  public Student get(long id){}
  }
JAX-WS operates according to services (e.g.: initiateInvoiceWorkflow, performPayment, etc).

  public interface AccountingService{

    @WebMethod(operationName="initiateInvoiceWorkflow" ...)

    Invoice initiateInvoiceWorkFlow();

  }

  // have your implementation...
So in theory, you can have something that's called StudentRepository where you can use that repository with both JAX-RS (REST) and JAX-WS (WebService) implementation easily (I've done this and it's quite straightforward) if your "Enterprise client" forces you to do so.

The important bit here is testing. You can easily test both JAX-RS and JAX-WS implementation in both unit-test or integration-test. You can easily do unit-test because you don't need to deploy them to the server: they're just normal Java classes. You can do integration test by deploying them to the server and generates the client implementation (in which I'll cover next).

The client-side implementation of JAX-RS is also similar to that of JAX-WS programming style.

In JAX-WS, you grab a WSDL, throw it to a generator tool that comes with JDK to generate the model (Invoice, Student, etc) and the proxy client-side to call the server-side. Very very straightforward, 5 minute job.

In JAX-RS, you'd use jersey-client to perform HTTP call as follow:

  // url  => is a string that points to JAX-RS end-point e.g.: student/1
  // header => json? xml? atom?
  // type => (typeof Student) (well... it's Java).
  Student student = Client.create().resource(url).accept(header).get(type);
Keep in mind that in the client-side, your Student class must have roughly the same structure and must be annotated using JAXB XML annotation (the client-side also relies on JAXB -> Jackson -> Java object conversion for the case of JSON, or just JAXB -> JAva object for the case of XML).

So no hacking using XPath or something like that (I work in Ruby once in a while and when I read some of the 3rd-party libraries/gems that implement client-side API against popular service provider, most of the implementations do brute force using XPath querying node element and stuff).

PS: Excuse me for the poor formatting, where can I learn to format my comment?

UPDATE: fix the format.

Oh and one more thing: JAX-RS (Jersey) is just an implementation on top of Servlet. So all of your previous knowledge regarding to Servlet (Context, deployment, URL, Filter) will be definitely useful.



Thanks for the clarification and expansion! I was worried about dumping too many details on people while trying to convince them that Java can be easy too ;-)

But you're absolutely right on most of these points.

All the formatting tricks are here: http://news.ycombinator.com/formatdoc


Loved the original article and the expansion in this post made it even better. Would you guys recommend any good books that cover JEE6 web services thoroughly? I'm an experienced java guy but have not dealt much with web services.


None. I usually rely on the source code + javadocs. I might be biased but I find that most Java source code and documentations tend to be easier to read due to common practices employed by newer projects.

I can only give you pointers to JavaEE 6 books (overall).

Beginning GF3 is an OK introduction to JavaEE 6: http://www.amazon.com/Beginning-GlassFish-Experts-Voice-Tech...

Oracle Tutorial (formerly SUN tutorial) for JavaEE 6 is another OK one (reviews were meh, but you've got limited choice so...) http://docs.oracle.com/javaee/6/tutorial/doc/

I saw Amazon has the print edition and another one to be in stock by mid 2012 (Advanced Java EE 6...)

Here's another book covering Java EE6 (intro):

http://www.amazon.com/Java-EE-GlassFish-Application-Server/d...

Seems to cover Servlet more than Apress book (first book).

You probably would need to know JPA 2.0 (ORM) as well.

And some more "best practices/real world-ish" tutorials:

http://www.amazon.com/World-Night-Hacks-Dissecting-Business/...

Good luck.

http://www.amazon.com/Pro-JPA-Mastering-Persistence-Technolo...


Java is still verbose though :).

If JavaEE can reduce JPA2/Model boiler plate code, then it'll be much improved.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: