From cabf8283f88f975d4f2da74cc1a2634fdf8bfc2a Mon Sep 17 00:00:00 2001
From: Daniel <59575049+lumijiez@users.noreply.github.com>
Date: Sun, 3 Nov 2024 22:27:11 +0200
Subject: [PATCH] upgraded http request body
---
pom.xml | 11 +++++-
.../lumijiez/core/http/HttpRequestBody.java | 34 +++++++++++++++++++
2 files changed, 44 insertions(+), 1 deletion(-)
create mode 100644 src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java
diff --git a/pom.xml b/pom.xml
index ea1ee91..254ecd8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -47,7 +47,16 @@
gson
2.11.0
-
+
+ com.fasterxml.jackson.core
+ jackson-databind
+ 2.18.1
+
+
+ com.fasterxml.jackson.dataformat
+ jackson-dataformat-xml
+ 2.15.2
+
org.postgresql
postgresql
diff --git a/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java b/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java
new file mode 100644
index 0000000..2374103
--- /dev/null
+++ b/src/main/java/io/github/lumijiez/core/http/HttpRequestBody.java
@@ -0,0 +1,34 @@
+package io.github.lumijiez.core.http;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
+
+import java.io.IOException;
+
+public class HttpRequestBody {
+ private final String rawContent;
+ private final HttpContentType contentType;
+ private static final ObjectMapper jsonMapper = new ObjectMapper();
+ private static final XmlMapper xmlMapper = new XmlMapper();
+
+ public HttpRequestBody(String rawContent, HttpContentType contentType) {
+ this.rawContent = rawContent;
+ this.contentType = contentType;
+ }
+
+ public T parseAs(Class clazz) throws IOException {
+ return switch (contentType) {
+ case APPLICATION_JSON -> jsonMapper.readValue(rawContent, clazz);
+ case APPLICATION_XML -> xmlMapper.readValue(rawContent, clazz);
+ default -> throw new IOException("Unsupported content type for parsing: " + contentType);
+ };
+ }
+
+ public String getRawContent() {
+ return rawContent;
+ }
+
+ public HttpContentType getContentType() {
+ return contentType;
+ }
+}