TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The Modern Java Ecosystem (for the Sinatra or web.py lover)

127 pointsby smanekover 13 years ago

10 comments

hello_motoover 13 years ago
I'd like to add a few things here.<p>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).<p>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:<p><pre><code> // will return XML or JSON depending on the request from the client. @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) </code></pre> This is a big win if you need to support both.<p><pre><code> - Object to XML conversion is done by JAXB. - Object to JSON conversion is done by Jackson via JAXB. </code></pre> Jersey is part of JavaEE 6 standard (part of your Application Server if it supports it).<p>What's lacking from JavaEE 6 is an MVC framework which is targeted for JavaEE 7.<p>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.<p>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:<p><pre><code> // Rough pseudo-code, omitting a few JAX-RS annotation public class StudentResource{ @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public &#60;List&#62; all(){} @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) public Student get(long id){} } </code></pre> JAX-WS operates according to services (e.g.: initiateInvoiceWorkflow, performPayment, etc).<p><pre><code> public interface AccountingService{ @WebMethod(operationName="initiateInvoiceWorkflow" ...) Invoice initiateInvoiceWorkFlow(); } // have your implementation... </code></pre> 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.<p>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).<p>The client-side implementation of JAX-RS is also similar to that of JAX-WS programming style.<p>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.<p>In JAX-RS, you'd use jersey-client to perform HTTP call as follow:<p><pre><code> // url =&#62; is a string that points to JAX-RS end-point e.g.: student/1 // header =&#62; json? xml? atom? // type =&#62; (typeof Student) (well... it's Java). Student student = Client.create().resource(url).accept(header).get(type); </code></pre> 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 -&#62; Jackson -&#62; Java object conversion for the case of JSON, or just JAXB -&#62; JAva object for the case of XML).<p>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).<p>PS: Excuse me for the poor formatting, where can I learn to format my comment?<p>UPDATE: fix the format.<p>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.
评论 #3304989 未加载
scorchinover 13 years ago
For those looking for something more like Sinatra in Java, check out webbit: <a href="https://github.com/joewalnes/webbit" rel="nofollow">https://github.com/joewalnes/webbit</a><p>Here's an example of a simple Websocket chat server: <a href="https://gist.github.com/1421652" rel="nofollow">https://gist.github.com/1421652</a><p><i>Disclaimer: It's an open source project that I have some commits to</i>
评论 #3307953 未加载
评论 #3306823 未加载
andrewl-hnover 13 years ago
We use JAX-RS as a primary web framework in our current project and honestly I can't get happier! We use Guava for low-level basic stuff and Google GSON for JSON serialization. It seems to be on par with Jackson from ease-of-use point of view:<p>MyType myVar = gson.fromJson(jsonString);<p>JPA on a back, some basic IoC in the middle (nothing fancy). Mockito and jUnit for tests.<p>All in all it's just a bunch of small layered classes with some bits of annotations and no xml. Well, one web.xml file with single servlet declaration for JAX-RS :)<p>With good IDE (they all are great this days), Maven for dependencies, well-known practices and modern APIs Java isn't that scary or cumbersome anymore. It's just a nice small language. A bit verbose without closures and type inference but still Ok.<p>I should note, though, that we generally use our Java layer as a service provider. We keep main bits of logic on a client side in JavaScript and call Java either for transactions and data, or in cases when something is hard to do on a client. It's just easier that way.
评论 #3304983 未加载
评论 #3305484 未加载
jshenover 13 years ago
I'm not a big fan of the heavy use of annotations. When an annotation does something other than what I expected it becomes a pain in the butt to figure out why. Also, back in the day, jersey and guice didn't get along because their use of annotations on the same methods conflicted because they both wanted to drive the bus so to speak.<p>This has been resolved, but it left a bad taste in my mouth because there isn't a good solution other than waiting for them to fix it.
评论 #3305480 未加载
ww520over 13 years ago
For web, Play Framework and Japid template are very good.<p>For distributed clustering, Hazelcast is amazingly good.<p>For massively scalable network services, Netty does a fantastic job.
评论 #3306377 未加载
评论 #3305509 未加载
mattmillerover 13 years ago
This is very cool. I am tied to Java b/c of Lucene and the Hadoop ecosystem as well. Some more things I would like to see:<p>-Templating. Something like SimpleTemplate Engine in Bottlepy. <a href="http://bottlepy.org/docs/dev/stpl.html" rel="nofollow">http://bottlepy.org/docs/dev/stpl.html</a><p>-How do I get rid of Tomcat at least for development. I would like to run java -jar MyApp.jar -Dport=8080 and get a live running webapp in my dev environment.
评论 #3305719 未加载
评论 #3305706 未加载
评论 #3305194 未加载
评论 #3306006 未加载
评论 #3305469 未加载
icandoitbetterover 13 years ago
&#62;Javascript (which is basically just a Lisp with infix syntax ;-))<p>No, it's not.
评论 #3306317 未加载
rbanffyover 13 years ago
In general, the code is still impressively verbose. OTOH, it's incredibly short for Java code. I suspect that line/feature added count will increase more quickly than it would with Python or Ruby and it will quickly become a nightmare scenario indistinguishable from a more traditional Java web approach.
评论 #3304973 未加载
评论 #3304642 未加载
评论 #3305589 未加载
zmmmmmover 13 years ago
For me, the modern Java ecosystem is more about a sweet combination of groovy at the edges with a minimalist Java core. it seems to be the best combo of static and dynamic typing and capture a lot of the best of both worlds without buying into too much religion or "enterprise" type complexity.
swahover 13 years ago
But Play 2.0 is moving away from Java Servlets, IIRC... how do the modern solutions compare in that regard?
评论 #3309136 未加载