Echidna is supposed to solve our problem of a missing simple and lightweight REST Framework.
- Install Maven
- Clone this repo
- Install:
mvn clean install
Maven repositories
<repositories> <!-- Klauke Enterprises Releases --> <repository> <id>klauke-enterprises-maven-releases</id> <name>Klauke Enterprises Maven Releases</name> <url>https://repository.klauke-enterprises.com/repository/maven-releases/</url> </repository> <!-- Klauke Enterprises Snapshots --> <repository> <id>klauke-enterprises-maven-snapshots</id> <name>Klauke Enterprises Maven Snapshots</name> <url>https://repository.klauke-enterprises.com/repository/maven-snapshots/</url> </repository> </repositories>
Maven dependencies
Echidna:
<dependency> <groupId>de.d3adspace.echidna</groupId> <artifactId>echidna-server</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
Server:
EchidnaConfig config = new EchidnaConfigBuilder() .setServerPort(80) .setServerHost("localhost") .setResourceClasses(Collections.singletonList(new ExampleResource())) .createEchidnaConfig(); EchidnaServer echidnaServer = EchidnaServerFactory.createEchidnaServer(config); echidnaServer.start();
Resource Example:
@Path("/rest/v1") public static class ExampleResource { @GET @Path("/user/del") @Consumes("text/html") @Produces("text/plain") public HTTPResponse onUserDel(HTTPRequest request) { return HTTPResponse.newBuilder() .setStatus(HTTPStatus.OK) .setBody(HTTPBody.fromString("Hey")) .setHeaders(new HTTPHeaders()) .createHTTPResponse(); } @GET @Path("/user/del/{userId}") @Consumes("text/html") @Produces("text/plain") public HTTPResponse onUserDelId(HTTPRequest request, String userId) { return HTTPResponse.newBuilder() .setStatus(HTTPStatus.OK) .setBody(HTTPBody.fromString("Hey du " + userId)) .setHeaders(new HTTPHeaders()) .createHTTPResponse(); } @POST @Path("/user/add") @Consumes("text/html") @Produces("text/plain") public HTTPResponse onUserDelIdPost(HTTPRequest request, @PostKey("test") String userId) { System.out.println("Parameter: " + userId); return HTTPResponse.newBuilder() .setStatus(HTTPStatus.OK) .setBody(HTTPBody.fromString("Hey du " + userId)) .setHeaders(new HTTPHeaders()) .createHTTPResponse(); } }
Client Example:
EchidnaClient client = new EchidnaClient(); HTTPRequest request = HTTPRequest .newBuilder() .setLocation("rest/v1/user/add") .setMethod(HTTPMethod.POST) .setVersion("HTTP/1.1") .setHeaders(new HTTPHeaders()) .createHTTPRequest(); HTTPBody body = HTTPBody.newBodyBuilder("test", "IchBinDerBeste").build(); HTTPResponse response = null; try { response = client.request(new URL("http://localhost/rest/v1/user/add"), request, body); } catch (MalformedURLException e) { e.printStackTrace(); } System.out.println("Got Response: " + response);