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:
Teriuihi 2024-08-06 23:58:15 +02:00
parent 2c3cb48667
commit 2ad194fab2
7 changed files with 62 additions and 16 deletions

View File

@ -4,11 +4,14 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Add REST annotations to StaffAppController&#10;&#10;Introduce `@RestController` and `@RequestMapping` annotations to the `StaffAppController` class. This change standardizes the API endpoint and ensures all methods within this class are mapped correctly under `/api/apply`.">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<list default="true" id="ce59df2a-8d56-446a-867b-80e627daf479" name="Changes" comment="Add REST annotations and refactor properties file handling&#10;&#10;Introduced `@RestController` and `@RequestMapping` in `StaffAppController` for standardized API endpoints. Refactored properties file handling in `PropertiesLoader` and `PropertiesWriter` to simplify file creation logic.">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/properties/PropertiesLoader.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/properties/PropertiesLoader.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/properties/PropertiesWriter.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/properties/PropertiesWriter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/build.gradle" beforeDir="false" afterPath="$PROJECT_DIR$/build.gradle" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/Form.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/StoreFormQuery.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/form/StoreFormQuery.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/verify_mail/FormQuery.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/verify_mail/FormQuery.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/alttd/forms/verify_mail/VerifyController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/alttd/forms/verify_mail/VerifyController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.properties" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/application.properties" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -366,7 +369,7 @@
<workItem from="1722786481232" duration="617000" />
<workItem from="1722801254695" duration="6135000" />
<workItem from="1722879625782" duration="358000" />
<workItem from="1722973564053" duration="5260000" />
<workItem from="1722973564053" duration="6695000" />
</task>
<task id="LOCAL-00001" summary="Initial commit for site for forms">
<option name="closed" value="true" />
@ -520,7 +523,23 @@
<option name="project" value="LOCAL" />
<updated>1722975785066</updated>
</task>
<option name="localTasksCounter" value="20" />
<task id="LOCAL-00020" summary="Update CORS allowed origins configuration&#10;&#10;Consolidated the allowed origins into a single `allowedOrigins` call. This ensures both local and production environments are correctly configured for CORS.">
<option name="closed" value="true" />
<created>1722978999091</created>
<option name="number" value="00020" />
<option name="presentableId" value="LOCAL-00020" />
<option name="project" value="LOCAL" />
<updated>1722978999091</updated>
</task>
<task id="LOCAL-00021" summary="Add REST annotations and refactor properties file handling&#10;&#10;Introduced `@RestController` and `@RequestMapping` in `StaffAppController` for standardized API endpoints. Refactored properties file handling in `PropertiesLoader` and `PropertiesWriter` to simplify file creation logic.">
<option name="closed" value="true" />
<created>1722979026190</created>
<option name="number" value="00021" />
<option name="presentableId" value="LOCAL-00021" />
<option name="project" value="LOCAL" />
<updated>1722979026190</updated>
</task>
<option name="localTasksCounter" value="22" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -558,6 +577,8 @@
<MESSAGE value="Refactor HTML generation in ContactFormData&#10;&#10;Extract the HTML generation logic to a reusable method in the Form class. This change reduces code duplication and enhances maintainability by centralizing the table generation functionality." />
<MESSAGE value="Add Staff Application Form handling&#10;&#10;Introduced `StaffAppController` and `StaffAppFormData` to handle staff application form submissions. The controller now stores user data, initiates email verification, and provides appropriate responses based on the verification outcome. Additionally, updated `application.properties` and cleaned up IntelliJ workspace.xml." />
<MESSAGE value="Add REST annotations to StaffAppController&#10;&#10;Introduce `@RestController` and `@RequestMapping` annotations to the `StaffAppController` class. This change standardizes the API endpoint and ensures all methods within this class are mapped correctly under `/api/apply`." />
<option name="LAST_COMMIT_MESSAGE" value="Add REST annotations to StaffAppController&#10;&#10;Introduce `@RestController` and `@RequestMapping` annotations to the `StaffAppController` class. This change standardizes the API endpoint and ensures all methods within this class are mapped correctly under `/api/apply`." />
<MESSAGE value="Update CORS allowed origins configuration&#10;&#10;Consolidated the allowed origins into a single `allowedOrigins` call. This ensures both local and production environments are correctly configured for CORS." />
<MESSAGE value="Add REST annotations and refactor properties file handling&#10;&#10;Introduced `@RestController` and `@RequestMapping` in `StaffAppController` for standardized API endpoints. Refactored properties file handling in `PropertiesLoader` and `PropertiesWriter` to simplify file creation logic." />
<option name="LAST_COMMIT_MESSAGE" value="Add REST annotations and refactor properties file handling&#10;&#10;Introduced `@RestController` and `@RequestMapping` in `StaffAppController` for standardized API endpoints. Refactored properties file handling in `PropertiesLoader` and `PropertiesWriter` to simplify file creation logic." />
</component>
</project>

View File

@ -27,8 +27,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation group: 'javax.mail', name: 'mail', version: '1.4'
implementation 'org.springframework.boot:spring-boot-starter-logging'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.12.3'
}
test {

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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 admin@alttd.com");
} 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 admin@alttd.com");
} 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 admin@alttd.com");
}
MailForm.sendForm("akastijn@alttd.com", 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 admin@alttd.com");
}
})
.orElse(ResponseEntity.ok(result.failReason()))
).exceptionally(throwable -> ResponseEntity.internalServerError()

View File

@ -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