https://github.com/IntellectualSites/HTTP4J
Simple & Lightweight Java 8 HTTP Client
https://github.com/IntellectualSites/HTTP4J
http-client java java-8 java-http-client java-http-request java-rest
Last synced: 18 days ago
JSON representation
Simple & Lightweight Java 8 HTTP Client
- Host: GitHub
- URL: https://github.com/IntellectualSites/HTTP4J
- Owner: IntellectualSites
- License: mit
- Created: 2020-07-01T16:09:56.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-11-19T02:06:33.000Z (about 1 month ago)
- Last Synced: 2025-11-19T04:09:00.354Z (about 1 month ago)
- Topics: http-client, java, java-8, java-http-client, java-http-request, java-rest
- Language: Java
- Homepage:
- Size: 476 KB
- Stars: 51
- Watchers: 5
- Forks: 3
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-java - HTTP4J
README
# HTT4J
## Description
This is a simple, lightweight and tiny wrapper for Java's HttpURLConnection. It has no external
dependencies and is written for Java 8.
It comes with a entity mapping system (serialization and deserialization for request and response bodies)
with optional mappings for third party libraries (currently supporting: GSON).
### Rationale
Most HTTP client for Java are either built for Java 11+, or have a large amount of dependencies,
which means that in order to use them, one needs to built a fatjar that often end up being huge.
This aims to offer a nicer way to interact with the Java 8 HTTP client, without having to double the
size of the output artifacts.
## Usage
### Repository
Releases are published to the central repository, snapshots are published to S01 OSS Sonatype.
```kotlin
repositories {
mavenCentral()
}
dependencies {
implementation("com.intellectualsites.http:HTTP4J:VERSION)
}
```
Ensure to relocate HTTP4J using the gradle shadow plugin to your classpath.
```xml
com.intellectualsites.http
HTTP4J
VERSION
```
### Code
**JavaDocs:** [https://javadoc.io/doc/com.intellectualsites.http/HTTP4J](https://javadoc.io/doc/com.intellectualsites.http/HTTP4J)
All requests are done using an instance of `com.intellectualsites.http.HttpClient`:
```java
HttpClient client = HttpClient.newBuilder()
.withBaseURL("https://your.api.com")
.build();
```
The client also take in a `com.intellectualsites.http.EntityMapper` instance. This
is used to map request & response bodies to Java objects. By default, it includes a mapper
for Java strings.
```java
EntityMapper entityMapper = EntityMapper.newInstance()
.registerDeserializer(JsonObject.class, GsonMapper.deserializer(JsonObject.class, GSON));
```
The above snippet would create an entity mapper that maps to and from Java strings, and
from HTTP response's to GSON json objects.
This can then be included in the HTTP client by using `.withEntityMapper(mapper)` to
be used in all requests, or added to individual requests.
HTTP4J also supports request decorators, that can be used to modify each request. These are
added by using:
```java
builder.withDecorator(request -> {
request.doSomething();
});
```
The built client can then be used to make HTTP requests, like such:
```java
client.post("/some/api").withInput(() -> "Hello World")
.onStatus(200, response -> {
System.out.println("Everything is fine");
System.out.println("Response: " + response.getResponseEntity(String.class));
})
.onStatus(404, response -> System.err.println("Could not find the resource =("))
.onRemaining(response -> System.err.printf("Got status code: %d\n", response.getStatusCode()))
.onException(Throwable::printStackTrace)
.execute();
```
#### Exception Handling
HTTP4J will forward all RuntimeExceptions by default, and wrap all other exceptions (that do not
extend RuntimeException) in a RuntimeException.
By using `onException(exception -> {})` you are able to modify the behaviour.
#### Examples
More examples can be found in [HttpClientTest.java](https://github.com/IntellectualSites/HTTP4J/blob/main/src/test/java/com/intellectualsites/http/HttpClientTest.java)
## Projects using HTTP4J:
**[IntellectualSites/Arkitektonika-Client](https://github.com/IntellectualSites/Arkitektonika-Client)**: Client for the Arkitektonika API
**[broccolai/tickets](https://github.com/broccolai/tickets)**: Discord bot