I updated my application to Spring Boot 3 and trying the existing controller for a PUT request and it does not work for a json.
Regardless of the values that I sent even in controller level it always set the default values ans never updates the values.
This is the values that I sent in postman as PUT request
{
"siteId": "1a6683e1-c9a7-4887-b400-b7faafa5819f",
"version": 10,
"visibilityPreferences": {
"featureA": true,
"featureB": false,
"featureC": false,
"featureD": true,
"featureE": false,
"featureF": false,
"featureG": true,
"featureH": false,
"featureI": false,
"featureJ": false,
"featureK": false,
"featureL": false
}
}
and this is the response that I get
{
"siteId": "1a6683e1-c9a7-4887-b400-b7faafa5819f",
"version": 10,
"preferences": {
"visibilityPreferences": {
"featureA": false,
"featureB": false,
"featureC": false,
"featureD": false,
"featureE": false,
"featureF": false,
"featureG": false,
"featureH": false,
"featureI": false,
"featureJ": false,
"featureK": false,
"featureL": false
}
}
}
and as it is seen all of them false.
Then this is the code part
Controller
@RestController
@RequestMapping("/api/sites")
class PreferenceController(private val preferenceService: PreferenceService) {
@AuthorizeUser
@PutMapping("/{siteId}/preferences")
fun updatePreferences(@PathVariable siteId: UUID, @RequestBody preferencesVisibility: PreferencesVisibility): Preference =
preferenceService.updatePreferences(siteId, preferencesVisibility)
}
PreferencesVisibility class definition
class PreferencesVisibility : Serializable {
var visibilityPreferences = VisibilityPreferences()
}
VisibilityPreferences class
class VisibilityPreferences : Serializable {
var featureA: Boolean? = true
var featureB: Boolean? = true
var featureC: Boolean? = true
var featureD: Boolean? = true
var featureE: Boolean? = true
var featureF: Boolean? = true
var featureG: Boolean? = true
var featureH: Boolean? = true
var featureI: Boolean? = true
var featureJ: Boolean? = true
var featureK: Boolean? = true
var featureL: Boolean? = true
var featureM: Boolean? = true
}
Also I added some converter class for Spring Boot 3
Preference Entity class
@Entity
@Table(name = "preferences")
class Preference() : BaseEntity() {
@Convert(converter = VisibilityPreferencesJsonConverter::class)
@Column(columnDefinition = "jsonb")
var preferences: VisibilityPreferences = VisibilityPreferences()
}
Converter Class
@Converter(autoApply = true)
class VisibilityPreferencesJsonConverter : GenericJsonConverter<VisibilityPreferences>() {
override val typeReference = object : TypeReference<VisibilityPreferences>() {}
}
Generic Converter Class
abstract class GenericJsonConverter<T> : AttributeConverter<T, String> {
private val objectMapper = ObjectMapper()
abstract val typeReference: TypeReference<T>
override fun convertToDatabaseColumn(attribute: T): String =
try {
objectMapper.writeValueAsString(attribute)
} catch (e: Exception) {
throw IllegalArgumentException("Error converting object to JSON", e)
}
override fun convertToEntityAttribute(dbData: String): T =
try {
objectMapper.readValue(dbData, typeReference)
} catch (e: Exception) {
throw IllegalArgumentException("Error converting JSON to object", e)
}
}
Regardless of the values gives it always put default values,
Even I try to debug and observe values in controller level I observe default values,
So I am not sure how to change it because is it related with the converter or should I add other annotations in the entity class,
This is just a simply cascaded JSON request, how could I achieve this in Spring Boot 3.
Thanks for advance
1