doom
A minimal java web framework to quickly make REST APIs. This project is for learning purposes.
Science Score: 18.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
○codemeta.json file
-
○.zenodo.json file
-
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (13.1%) to scientific vocabulary
Repository
A minimal java web framework to quickly make REST APIs. This project is for learning purposes.
Basic Info
Statistics
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
Doom
A simple minimal java web framework. It's a web framework which I have created solely for learning purposes.
Table of content
- Getting Started
- Installation
- Routing
- HTTP Request Method Annotations
- How to define Handler
- URL Path Parameters
- URL Query Parameters
- Form Data
- Request Class
- Response Class
- Middleware
Getting Started
java
class Application {
public static void main(String[] args) {
DoomServer server = new DoomServer(Application.class);
server.start();
}
}
Installation
Download the JAR from release tab of github and add it to your project directly or using gradle.
using gradle:
Add downloaded JAR to libs folder and below snippet to build.gradle
groovy
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
Routing
To Define API end-points in this framework mark class with @PATH(Here, @PATH is like controller) annotation and give a base url to it, now methods defined within this class with HttpMethod annotations will be API end-points.
java
@Path("/api/example")
public class ExampleResource {
@POST("/resource")
public Response getNames(Request request) {
System.out.println("Hello world" + request.getPath());
return new Response("Hello world!");
}
}
You just need to mark class with @PATH and everything will be taken care of by the Doom.
HTTP Request Method Annotations
Mark methods inside controller with these annotations to tell framework what type of http request this handler should cater to.
- @GET
- @POST
- @PUT
- @PATCH
- @DELETE
- @HEAD
All of these annotations take path as value and default value of path is \. This path is appended after base path provided in controller.
## How to define Handler
The structure of all Handlers should follow these rulse:
- The function should be marked with one of the HttpMethod annotations.
- The function should accept only one parameter i.e. of type Request.
- The function should return only one parameter i.e. of type Response.
java
@POST("/path")
public Response functionNamr(Request request) {
return new Response("response");
}
URL Path Parameters
The Path parameter can be defined by using curly braces {} inside value of HttpMethod Annotations.
@GET("/example/{name}/{id}")
public Response functionNamr(Request request) {
System.out.println(request.getPathParam("name"));
return new Response("response");
}
To access path param use this method of Request class
Request.getPathParam("param") //returns value of path param if it exists else null is returned
URL Query Parameters
The URL after ? contains query parameters.
e.g. if url is /example?name=dhruv
@GET("/example")
public Response functionNamr(Request request) {
System.out.println(request.getQueryParam("name"));
return new Response("response");
}
To access query param use this method of Request class
Request.getQueryParam("param") //returns value of query param if it exists else null is returned
Form Data
To access Form Data, we use getRequestBody method of Request Class. Form data contains String and File as value.
```java @POST("/resource/multi") public Response postExampleWithForm(Request request) { MultiPart body = (MultiPart) request.getRequestBody().getBody(); System.out.println(body.getText("name"));
File file = body.getFile("file");
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
return new Response("Hello world!");
} ```
**Note*: The File received from MultiPart.getFile() is a Temporary file and one must save it to desired path if required.
Request Class
This class is used to access: - Query Parameters - Path Parameters - Body - Form data - Path information - Headers - and all things related to incoming request
Response Class
This class in used to return Response to the client. Set status code, body and header of your response using this class.
Middleware
It is used to call a method before request handler.
It can be defined: - Globally for all calls - Controller Scoped i.e. applied before all calls defined inside specific controller - Request Scoped
@Middleware annotation is used to declare middlewares to be appied. It takes list of Middleware class as value.
To define a middleware implement MiddlewareHandler interface.
```java
public class ExampleMiddleware implements MiddlewareHandler {
@Override
public Response handle(Request req) {
System.out.println("Middleware executed");
return new Response("ok");
}
}
@MiddleWare({ExampleMiddleware.class}) @Path("/api/example") public class ExampleResource { @POST("/resource") @MiddleWare({ExampleMiddleware.class}) public Response getNames(Request request) {
System.out.println("Hello world" + request.getPath());
return new Response("Hello world!");
}
} ```
To define middleware globally: ```java DoomServer server = new DoomServer();
server.addGlobalMiddleWare(new ExampleMiddleware());
//or using lambda server.addGlobalMiddleWare(req -> {
}); ```
Note: Middleware will only allow request to go further if 200 status code is returned in middleware.
Owner
- Name: Dhruv garg
- Login: Dhruv-Garg79
- Kind: user
- Location: India
- Website: www.dhruvgarg.dev
- Repositories: 65
- Profile: https://github.com/Dhruv-Garg79
Polyglot | Android | Flutter | Backend - Node, Java, PHP | some web FE
Citation (citation.txt)
https://docs.gradle.org/current/samples/sample_building_java_libraries.html https://www.baeldung.com/java-custom-annotation https://stackoverflow.com/questions/6642909/providing-java-library-but-hiding-some-classes https://github.com/Simonwep/java-express https://www.hackerearth.com/practice/notes/asynchronous-servlets-in-java/ channel - Cyecize https://www.youtube.com/watch?v=JHxFJwxb0FQ
GitHub Events
Total
Last Year
Dependencies
- commons-fileupload:commons-fileupload 1.4 implementation
- javax.servlet:javax.servlet-api 4.0.1 implementation
- org.json:json 20200518 implementation
- org.junit.jupiter:junit-jupiter-api 5.6.2 testImplementation
- org.junit.jupiter:junit-jupiter-engine 5.6.2 testImplementation
- org.mockito:mockito-core 3.5.7 testImplementation
- org.mockito:mockito-junit-jupiter 3.5.7 testImplementation
- surefire:surefire 1.4 testImplementation