Update Jackson config and refactor JSON handling
Introduced Jackson dependencies to replace Gson for JSON processing. Updated application properties and controllers to handle Jackson-specific exceptions. Refactored form serialization to use Jackson's `ObjectMapper` for better date handling and consistency.
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
package com.alttd.forms.form;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
public abstract class Form {
|
||||
|
||||
public String toJsonString() {
|
||||
return new Gson().toJson(this);
|
||||
public String toJsonString() throws JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.registerModule(new JavaTimeModule());
|
||||
return objectMapper.writeValueAsString(this);
|
||||
}
|
||||
|
||||
public abstract String toHtml();
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -41,6 +42,9 @@ public class StoreFormQuery {
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed insert form query for: " + form, e);
|
||||
return Optional.empty();
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Invalid class for JSON, failed insert form query for: " + form, e);
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,9 @@ package com.alttd.forms.verify_mail;
|
||||
import com.alttd.forms.contact.ContactFormData;
|
||||
import com.alttd.forms.database.DatabaseConnection;
|
||||
import com.alttd.forms.form.Form;
|
||||
import com.google.gson.Gson;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -53,6 +55,9 @@ public class FormQuery {
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.error("Invalid form class in database", e);
|
||||
return Optional.empty();
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Invalid form json in database", e);
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
logger.error("Failed select form query for form with id: {}", formId, e);
|
||||
@@ -60,10 +65,12 @@ public class FormQuery {
|
||||
}
|
||||
}
|
||||
|
||||
private Form getForm(String className, String json) throws IllegalArgumentException {
|
||||
private Form getForm(String className, String json) throws IllegalArgumentException, JsonProcessingException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.registerModule(new JavaTimeModule());
|
||||
switch (className) {
|
||||
case "ContactFormData" -> {
|
||||
return new Gson().fromJson(json, ContactFormData.class);
|
||||
return objectMapper.readValue(json, ContactFormData.class);
|
||||
}
|
||||
default -> throw new IllegalArgumentException("Invalid form class name: " + className);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.alttd.forms.verify_mail;
|
||||
|
||||
import com.alttd.forms.mail.mail_forms.MailForm;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import jakarta.validation.Valid;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -51,12 +52,20 @@ public class VerifyController {
|
||||
} catch (URISyntaxException e) {
|
||||
logger.error("Unable to create URI for posting form", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to create URI for posting form, please contact us at [email protected]");
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
} catch (IOException | InterruptedException e) {
|
||||
logger.error("Unable to send form to Discord", e); //TODO more clear
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
}
|
||||
MailForm.sendForm("[email protected]", form);
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
try {
|
||||
return ResponseEntity.ok(form.toJsonString());
|
||||
} catch (JsonProcessingException e) {
|
||||
logger.error("Unable to send form to Discord, invalid JSON", e);
|
||||
return ResponseEntity.ok("Unable to send form to Discord, please contact us at [email protected]");
|
||||
}
|
||||
})
|
||||
.orElse(ResponseEntity.ok(result.failReason()))
|
||||
).exceptionally(throwable -> ResponseEntity.internalServerError()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
logging.level.com.alttd.forms=warn
|
||||
server.port=8002
|
||||
spring.jackson.date-format=yyyy-MM-dd
|
||||
spring.jackson.date-format=yyyy-MM-dd
|
||||
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
|
||||
Reference in New Issue
Block a user