upgraded http request body

This commit is contained in:
Daniel
2024-11-03 22:27:11 +02:00
parent 082aa17ba8
commit cabf8283f8
2 changed files with 44 additions and 1 deletions

11
pom.xml
View File

@@ -47,7 +47,16 @@
<artifactId>gson</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>

View File

@@ -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> T parseAs(Class<T> 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;
}
}