r/javahelp • u/[deleted] • Feb 18 '17
Which Frameworks to use to build a REST backend in Java?
Hi,
I would like to build a REST backend in Java for a software that has multiple front ends (Angular 2, Cli and APIs in other languages). The backend doesn't need to be "web-scale" as it is only used in intranets.
From my research so far, I understand that there is:
The Spring framework (and related stuff like Spring Boot that you need in order to use it productively)
The Play framework (which seems to be newer, cooler and better in general, but is actually a Scala framework .. so as a Java user, I might not have the best experience)
These two solutions are pretty fat, as they are both full blown web frameworks with many features I don't need for my goal (templating engine or incremental rebuilds for example).
I wonder if it is not better to implement the REST-backend using:
Netty Router (https://github.com/sinetja/netty-router) for the web server
JPA / Hibernate for persisting data to a DB
Jackson / Jersey for REST and JSON representation
Swagger for generating APIs
Assembling everything myself allows me to use whatever build system I want to use (Maven instead of sbt (Play forces me to use sbt)), and I know how everything in my application is put together (one less framework to learn).
Who has done this before and provide me some guidance?
Thank you!
3
u/nutrecht Lead Software Engineer / EU / 20+ YXP Feb 18 '17
I wonder if it is not better to implement the REST-backend using: Netty Router (https://github.com/sinetja/netty-router) for the web server
JPA / Hibernate for persisting data to a DB
Jackson / Jersey for REST and JSON representation
Swagger for generating APIs
So instead of using Spring Boot you're creating your own version of Spring Boot?
2
Feb 18 '17
This approach would at least be a good exercise to learn all the involved technologies. I don't like relying on huge frameworks and consider them as a black box (because then programming is just stitching together APIs, and that makes it extremely hard to reason about what's going on). As the person who's responsible for debugging the application if something goes wrong, so I should have a good answer on how everything works together.
4
u/nutrecht Lead Software Engineer / EU / 20+ YXP Feb 18 '17
This is no way to build a career. Spring is pretty transparent in how it works. Inventing your own wheel is a complete waste of time.
If you want to go thay way; by all means go for it. But as a Java dev 14 years in the trade I'd suggest you build stuff based on standard tools. Whoever comes after you to maintain stuff is not going to like your custom-built framework.
1
Feb 18 '17
I agree with you that reinventing the wheel is not an option in a professional environment, except for cases where you are exactly knowing what you are doing.
I'm not saying I want to reinvent Spring from scratch. As far as I understand Spring, it also relies on stuff like jackson, jersey, hibernate. I have to learn these technologies anyways if I want to understand what's going on.
I didn't write it in my initial posting, but the focus of the backend I want to build is not in the web part of it. I'm basically building a control panel for a larger application, so the REST part is just about some control / monitoring commands. That's why I don't really need all the crazy web features. Its just about an easy way to serve some REST calls, some simple abstraction for persisting state in a database.
So tl;dr: I don't want to build my own web framework, I don't have the time to learn a full-fledged web framework but I want something simple and effective. My current plan for a "weekend PoC" is probably Spark + Hibernate.
3
u/feral_claire Software Dev Feb 18 '17
If you want to do it for the sake of learning and building a framework then sure. If you want to actually make a product, don't reinvent the wheel. Go with existing frameworks and libraries. Just because you are using a framework, doesn't mean you should be clueless to how your application works.
2
u/pacificmint Feb 18 '17
The Play framework [...] as a Java user, I might not have the best experience
As far as I know Java was a bit of a second class citizen in Play 1.x, but with Play 2.x they made sure that Java is well supported too. (Disclaimer: I've only used Play with Scala, so no first hand experience if the above is really true.)
You might also look into the Spark Framework, it might be more lightweight than the others. (Not to be confused with Apache Spark, which is a big data framework.)
1
Feb 18 '17
I've spend some time implementing some basic REST calls using the Play framework, and the Java support is indeed quite nice with Play 2.5. However, you are basically forced to use sbt, and there you hit already barriers (like the java checkstyle plugin is not really maintained). Also, you get ultra long stack traces with lots of generated scala method calls etc. in between. I think stepping through the method calls with a debugger through the scala code is not a lot of fun.
The Spark Framework looks indeed quite interesting. I'll take a closer look.
0
u/dartalley Intermediate Brewer Feb 18 '17
If you know HTTP well and already have favorite libraries you can easily build your own mini framework in a weekend. I find Spring very difficult to debug and much prefer using lower level libraries directly.
1
u/ltdanimal Feb 18 '17
While it might be good simply from a learning perspective, I feel that there are some really good solutions out there so really no need to reinvent the wheel
1
u/dartalley Intermediate Brewer Feb 18 '17
There is no innovation if everyone just works with what they have ;). At a high level honestly frameworks dont give you much today. The lower level libraries have advanced pretty far and I don't find the need for frameworks.
I'm not saying they are bad, but blindly using them because it's the status quo is not an acceptable answer. Look at most languages, most python other than django are low level frameworks, most scala web servers are lower level, node lower level, Go lower level. Java and C# have these massive frameworks that take longer to learn than the underlying tech but the lower level libraries have advanced greatly. Just my opinion.
2
u/snot3353 COMPUTERS!!! Feb 19 '17
I used Play quite a bit from 2.2->2.4 and the Java support was on par with the Scala support 99.9% of the time. There was maybe one instance I can think of ever where a utility existed in one of the Scala libraries that was not easily accessible from the Java side. It's a really nice framework in general and I like it... the auto-recompile stuff is especially neat and while Spring has something similar, I've issues with it whereas in Play it always worked without a hitch.
4
u/errandum Feb 18 '17
I'll be brutally honest with you and say, no, it isn't.
I've used spring and spring boot and one things you're forgetting is that it is all modular. You use what you want, you don't need all the pieces. From my experience it is also light weight enough, my tomcat inside a vm boots in seconds.
Also, it's extremely popular, so any problem you ever have with anything will already have happened to someone, so the solution is usually one Google search away.
Furthermore, it is all done out of the box, your rest service will be up and running in an afternoon or less. The only technology I used that took less time and configuration was ruby on rails.
The only thing I don't know is swagger, I've seen it used successfully with apache cxf, I don't see why it wouldn't work with spring. Never did care about automatic documentation, since I'm usually implementing following documentation.
And obviously, doing it yourself would bring other considerations about security and best practices that spring can provide you with out of the box.
2
u/nutrecht Lead Software Engineer / EU / 20+ YXP Feb 18 '17
The only thing I don't know is swagger
There's a Spring Plugin for that, like for pretty much anything under the sun.
2
2
u/OffbeatDrizzle Feb 18 '17
You don't need to even think about netty unless you're doing "low level" network stuff.
Just use spring boot. It's all there for you and when you inevitably decide to use more features you won't all of a sudden be like "damn I wish I used spring boot"
1
1
u/dartalley Intermediate Brewer Feb 18 '17
I would say Jersey / Dropwizard are the best of the popular solutions. If you like the spark approach check out using Undertow directly. Its a low level web server that has nice routing and a very clean api.
If you prefer putting everything together yourself here is an example of how I do it. Jackson for json, Undertow for webserver, jOOQ for SQL, Typesafe Config for Configurations, Dropwizard Metrics for metrics / healthchecks. I find this to be very easy to use. It might take slightly longer to get going than Spring boot but you will have much more control in the long run and use exactly what you want.
Feel free to ignore my use of static everywhere and no DI. Its pretty easy to convert all the classes to use however you want.
https://www.stubbornjava.com/posts/lightweight-embedded-java-rest-server-without-a-framework
This example is pretty minimal but covers logging, metrics, exception handling, and json serialization.
EDIT: As far as web scale goes. Using lower level code will always be faster than using the big frameworks (They all sit on top of the same underlying web servers). It's just more work setting up.
1
u/shagieIsMe Extreme Brewer Feb 18 '17 edited Feb 19 '17
From over on Slant.co... its a bit dated and not as filled out as it could be, but What are the best frameworks for developing REST frameworks in Java?
7
u/[deleted] Feb 18 '17
Just go with Dropwizard. It has Jetty for web, Jersey for REST and has pretty much all you need for lightweight, fast and highly scaling REST apps. Especially if you use Java 8, it allows to do nice asynchronous processing with futures (these fit it's model really well).
I would recommend it personally over Spring Boot, but then it's a matter of preference. Swagger comes pretty much out of the box for it as well.