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; + } +}