简体   繁体   中英

REST API with Akka in 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.

目前有两种现代微服务和REST技术:Vert.x和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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM