upgraded http request body
This commit is contained in:
11
pom.xml
11
pom.xml
@@ -47,7 +47,16 @@
|
|||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.11.0</version>
|
<version>2.11.0</version>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.postgresql</groupId>
|
<groupId>org.postgresql</groupId>
|
||||||
<artifactId>postgresql</artifactId>
|
<artifactId>postgresql</artifactId>
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user