REST API with Akka in Java - java

I am trying to create my own REST-based API using Java and Akka. I have created my main algorithmic implementation using Akka already. My confusion is coming in the form of how to implement the REST part of this. Most examples and libraries I have seen are specifically for Scala, which I am at the moment trying to stay away from.
I see Spray is a good way to go, but I see it's supposed to be for Scala. However, I know Scala compiles down to Java Byte Code and Java should be able to call Scala and visa versa. Is it possible to do this with Spray? If so, are there any working examples or tutorials online? I am not having any luck anywhere.
Thanks for your help and time.

I would recommend you to use playframework 2.0. It is already integrated with akka and you can choose to write your code in Java or Scala. Its will be very easy to implement a REST-based API.

Actually, I went down the same exact path: wanted to use Akka for REST-based services implementation and did not want to use Scala.
Akka can be used with Play-mini, which gives you the Sinatra-like REST mapping without any of the ui stuff (you don't need).

You may also want to look at the Spray Framework http://spray.io/.
It might be the lightweight alternative to Play Framework.

Here is a github repo with Jersey 2 Rest Service using Akka actors for processing in a Java project based on Maven.
https://github.com/pofallon/jersey2-akka-java

I think you want to look at this:
https://www.typesafe.com/activator/template/akka-http-microservice
Simple (micro)service which demonstrates how to accomplish tasks typical for REST service using Akka HTTP. Project includes: starting standalone HTTP server, handling simple file-based configuration, logging, routing, deconstructing requests, unmarshalling JSON entities to Scala's case classes, marshaling Scala's case classes to JSON responses, error handling, issuing requests to external services, testing with mocking of external services.

There is an old entry (2010) on akka github for a akka-sample-rest-java example that would be really interesting. However it's removed in following versions, don't know why.
https://github.com/akka/akka-modules/tree/v1.0/akka-samples/akka-sample-rest-java/src/main/java/sample/rest/java
Since it has been unmaintained, most of the classes rely on old versions of akka and does not works with the most recent ones.

at the moment two modern microservices & REST technologies: Vert.x and dropwizard

Akka-HTTP is the defacto Spray 2.0 as this stackoverflow thread shows:
Spray, Akka-http and Play, Which is the best bet for a new HTTP/REST project
So, I would opt for choosing Akka-HTTP if REST based API for Akka was what you were looking for.

Related

Is there an OpenRasta like REST framework for Java?

The circumstances force me to start writing a java based server and I am looking for a REST framework for Java. I really like the way OpenRasta works - a REST framework for .NET, so my question - does anyone know a REST framework for Java which works similarly to OpenRasta?
Thanks.
EDIT
Unfortunately, I am not in a position to be an early adaptor, so well established frameworks have preference over emerging ones.
You want to use one of the implementations of the JAX-RS specification, one of the main ones being Jersey. The specification and it's implementations have been around for a long time and are tried and tested (as opposed to Play! which you don't seem to like due to it being a newer framework).
I used OpenRasta and then worked on Java projects using JAX-RS. It will feel very familiar and you will get the benefits of POJOs like you get from OpenRasta's use of POCOs. I feel it's a great choice for creating RESTful services. In fact, I would probably chose it over OpenRasta now but this is more due the benefits of working within a Unix environment than the frameworks themselves.
The play framework is a popular new choice for Scala and the JVM:
http://www.playframework.org/documentation/1.2/routes
One option is Play!, which is basically framework based off Ruby on Rails.
Second option you might be interested in is Lift, which also has really cool features like comet, AJAXy snippets, etc. But it is a little bit different than Play!.
As part of this project, I would recommend Restlet which is the first and most comprehensive REST framework for Java. It works on both client and server side and is indeed resource centric, not just a MVC framework adapted to REST.

RestKit in android?

I have used restkit framework in ios. Which is very powerful and provides features like caching and all. I am looking whether a similar framework is available in android?
I came across a framework called restlet. But didnt find any samples to work with.
Is there any frameworks out there for the same, for caching data from rest services? Also could some one direct me to good tutorials for restlet in android?
Thanks
In addition, there is this first application tutorial covering Restlet edition for Android:
http://restlet.org/learn/guide/2.2/introduction/first-steps/first-application
There is a sample to work :
http://restlet.org/learn/guide/2.2/introduction/first-steps/first-application
It should solve your problem
I wouldn't call it a framework, but there is a library called Robospice that supports Caching Rest Calls.
Here is the description they posted in their webpage:
RoboSpice is a modular android library that makes writing asynchronous
long running tasks easy. It is specialized in network requests,
supports caching and offers REST requests out-of-the box using
extension modules
Give Retrofit a shot. It's easy with plenty of customization available.
It also has OkHttp that can pair with it.
Also check out this SO link. It compares Retrofit to Android AsyncTasks and Volley. As far as speed goes, Retrofit wins hands down.
I think the RestTemplate included in Spring for Android is a good solution: http://static.springsource.org/spring-android/docs/1.0.x/reference/html/rest-template.html
There is a book that provides a tutorial: http://www.packtpub.com/spring-for-android-starter/book
Unlike Restkit which comes with backed in Core Data support, you would still have to find a separate solution if you want to plug your Android Spring REST client into your local sqlite db. It should be possible to use something like http://greendao-orm.com/ in connection with the Spring REST client.

Java and .NET application interoperability

I am having a Java application and a .NET application both residing in two different machines and need to design a communication layer between these two applications. Any inputs or ideas would be really helpful. Below mentioned is the nature of interaction between these two applications.
Java applications sends large amounts of data to the .NET application
Data latency should be kept to a minimum
.NET application should also be able to request for some data (synchronously/asynchronously)
The easyest way .Net and Java can talk is using Web-Services - we have done in my company with much success (using apache's cxf and standard code on the .Net side).
But if latency and size are the main requirements, you should use Sockets - both platforms offer a pretty extensive socketing frameworks and it would give you the best performance possible.
I think this can be done by setting up an xml webservices layer on the java side. You can use RestEasy for restful web services. Just my .2 cents.
Another alternative is some form of MOM (Message Oriented Middleware). There are a lot of implementations, but one to look at first might be ActiveMQ as it has both Java and C# bindings (among others).
I'm not saying this is better than using a web-service, it entirely depends on what your requirements are.
We have had good experiences with providing web services with JAX-WS (part of standard runtime in Java 6). They explicitly list .NET compatibility as a goal and is well supported in IDE's.
The Endpoint.publish() mechanism allow for small, simple deployments.
You can use Web Services. Jax-WS is the API in java that allow you to use it. As the implementation of this API I recommend metro (http://metro.java.net/), this already came with the SDK, and has a great integration with netbeans.
As already someone referred yet, you can use a socket, and create a communication channel on that, but this have some problems, starting with security. DonĀ“t use this in real life applications.
If you need help with this subject you can start reading this:
Getting started with JAX-WS
It really depends on your requirements. The simple way is generally Web services. However, if you want higher performance, or more fine-grained access to the API on the other platform, you might want to consider JNBridgePro (www.jnbridge.com).
Disclosure: I work for JNBridge.

What approach for java web service to use?

I'm developing a web application with multiple frameworks (spring, hibernate, spring-security, ZK for GUI), and using Tomcat as app server. I must say I have absolutely no experience with java web services technologies. Thing is, I will almost certainly have to expose number of services for some external applications in the near future, and I was wondering what would be the way to go (considering the frameworks I'm using)...
I saw and read various tutorials and some questions (link) regarding Axis, Axis2, JAX-WS... Thing that confuses me a little bit is that I don't know what is the common practice (if any) to integrate services within existing web application (mainly in the terms of project organization). As I see it now, these services that I need to implement will rely partially on the existing source code, so I don't know whether I should use completely separate project, or I can put it inside my existing web app folder (which I tried with Axis2, but don't know if it's a good practice).
Thanks.
How to organize the projects?
In general I agree with #ericacm, but there is one thing you should keep in mind... You said you're going to develop a number of services in the near future. You may come to a point at which you want to host the services on a separate server, e.g. for performance, availability or maintainability reasons. This may influence your decision of separating the projects. Furthermore, separation "enforces" loose coupling, but therefore introduces other challenges like session sharing across multiple WARs. It's a case-by-case decision.
If I were in your situation I'd first ask myself whether the service(s) logically belongs to the web application or not.
Implementation
When in comes to WS-* implementations you have to make 2 decisions:
Decide for an API to use; today, I can't see any reason for not going with JAX-WS together with JAXB as API, they work well and they are standardized.
Decide for a Framework; I've experience using Axis2 as well as METRO (keep in mind that JSE 1.6+ provides basic JAX-WS support). Both work well. It's fairly easy to change the frameworks if you use the JAX-WS APIs.
I have good experience with Spring-WS 2+ and manual Castor mapping . Is is easy but powerful combination.
Spring-ws 2:
provides contract-first development (specially good for the web app with number of services).
provides WS annotation
supports XML mapping (Castor, JaxB, etc)
Castor:
mapping based on xml configuration
allows map multiple messages (requests/responses) to one java object (based on xml configuration)
If you are using some Java EE 6 server, consider also JAXB for manual mapping:
mapping based on annotation
should be faster than Castor
allows map multiple messages (requests/response) to one java object (when you use java inheritance)
You can go ahead and put them into the same project. Each web service will be an additional interface and implementation class along with some configuration.
Since you are using Spring CXF is a good choice as a for JAX-WS as it integrates well with Spring. See this page as a starter.
Spring-WS is complex framework for simple web services. If you want to understand web services completely and to know the nuts and bolts of web services, learn Spring-WS. It is extremely flexible and provides lot of options.
Else, if you want simpler alternative use JAX-WS. Spring supports JAX-WS annotations. Refer to the section 17.5.7. Exporting web services using the JAX-WS RI's Spring support.
http://static.springsource.org/spring/docs/2.5.x/reference/remoting.html
Is it possible to create WS Server and WS Client manually (without generators) by JAX-WS? Specially if you are developing a big application you want to re-use objects but generators are generating a lot of classes that can be in 99% the same (for example if your app is WS Client and you have to connect to badly designed external WS Server). Is there some tutorial how co create ws manually?
There is a lot of reasons why I don't like generators and completely agree with http://ogrigas.eu/spring/2010/04/spring-ws-and-jaxb-without-a-code-generator

REST on GAE, what implementation to use?(RESTlet, jersey, other....)

Have somebody experience with using REST on GAE? Maybe you can advise some lightweight REST implementation for this.
I found that RESTlet said that they have GAE optimised implementation. RESTlet-GAE
Anybody try this?
I am currently using GAE plus Restlet 2.0M6 for a Goolge Apps Domain specific business project and find it very useful.
Integration with google Accounts and Data Api is easily done for obvious reasons.
However I could not get the XStream lib that is bundled with Restlet to work properly, so I use the very intuitive Simple framework instead for object serialization.
Another disadvantage of Restlet is its lack of JSP integration, which is unfortunate if you do not want to use more heavy-weight templating approaches.
So far I have used XSLTransformations to render browsable html representations, but am not fully satisfied with it yet and always looking for suggestions.
Update
I am now using Saxon to transform xml on the server. This is a good tutorial on how to use it with App Engine. It is in Jython but not too hard to convert.
Update2
When the xslt approach is too heavy, I take a different approach with the helper package from the Lift framework to enable enriched xhtml templating/binding. This method is based on Scala however.

Categories