Small library with HTTP common components like HttpStatus, MediaType, URIBuilder, etc.
Have no dependencies.
Java 17+ support.
implementation "io.goodforgod:http-common:1.0.0"<dependency> <groupId>io.goodforgod</groupId> <artifactId>http-common</artifactId> <version>1.0.0</version> </dependency>Library provides HttpStatus with code and reasons, all mostly used HttpStatus are present there for easier use.
HttpStatus status = HttpStatus.ACCEPTED; int code = status.code(); String reason = status.reason(); HttpStatus statusAccepted = HttpStatus.valueOf(201);Describes HttpMethod common methods of HTTP protocol.
final HttpMethod method = HttpMethod.of("get"); assertEquarls(HttpMethod.GET, method);Library provides MediaType class for correctly parsing and interpreting MediaType / ContentTypes.
There are plenty of widely used and preconfigured MediaTypes.
MediaType jsonMediaTypeUtf8 = MediaType.APPLICATION_JSON_UTF_8_TYPE; MediaType mediaType = MediaType.of("application/json"); Optional<MediaType> jsonMediaType = MediaType.ofExtension("json");Library provide URIBuilder for easier and safer way of building URI.
URIBuilder.of("https://api.etherscan.io").path("/api") .param("module", "block") .param("action", "getblockreward") .build()Describes HTTP headers and can be instantiated via different builders available.
final Map<String, List<String>> headerMap = new HashMap<>(); headerMap.put(HttpHeaders.CONNECTION, List.of("keep-alive")); headerMap.put(HttpHeaders.CONTENT_LENGTH, List.of("72")); headerMap.put(HttpHeaders.CONTENT_TYPE, List.of("application/json")); final HttpHeaders headers = HttpHeaders.ofMultiMap(headerMap); final Optional<Long> contentLength = headers.contentLength(); final Optional<MediaType> mediaType = headers.contentType();Exception that allow to format messages like SLF4J logger and other similar.
throw new FormattedException("Failed for {} with code {}", "Bob", 200);Resulted exception message:
Failed for Bob with code 200 This project licensed under the Apache License 2.0 - see the LICENSE file for details