creating controllers with akka-http easily
Supports the http verbs: @GET
, @POST
, @PUT
, @DELETE
. Also can handle json body or/and path params.
- akka-http-core
- akka-http-jackson
- java 1.8
-
Add maven dependency in your project:
todo akka-http-ext-controller 0.0.l -
Create controllers in your application:
public class UserController {
@GET
@Path("users/:id/details")
public Route details(String id) {
// getting pathParams
return Directives.complete("Showing details for user " + id);
}
@POST
@Path("users")
public Route save(User user) {
// parsing json body
return Directives.complete("Saving user " + user.getName());
}
}
- Register your controllers with the Routes.init(ActorSystem, port, controllers...), as follows:
system = ActorSystem.create();
Routes.init(system, 8080, new UserController());
Please note that all controllers must be declared at once (In the same Routes.init
call).
- Done! You can access your controller at http://localhost:8080/users/1/details
All actions must have:
- annotation to indicate the http verb
- annotation
@Path
to indicate the path of action - return a
akka.http.javadsl.server.Route
object, usually you will useakka.http.javadsl.server.Directives
for this.
using PathParams
When you use pathParams, the framework inject the arguments in the same order as received, for example:
For @Path("user/:id/purchases/:purchaseId/items/:itemId/details")
must have a controller with 3 String arguments like this public Route details(String id, String purchaseId, String itemId)
using JSON Body
When you use json body, the framework will try convert body content to object. An action in controller must receive a object, for example public Route update(User user)
. An error 500 will raise if parse fails.
using QueryString
TODO
$ mvn test
- add support to pathParams
- add support to json body
- add artifacts to the Central Repository
- add support to queryString
- add support to pathParams, json body and queryString when used in same time
- create annotation
@Controller
and make package scan to register controllers - create annotation
@Produces
to use with custom content-types likeapplication/vnd.api+json
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
- Nadilson - Initial work - nadilsons
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details