'org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: java.lang.String cannot be cast to java.lang.Enum
I have a Spring boot application. spring-boot-starter-parent 2.3.0.RELEASE
I'm trying to get a list of objects and expect to get something like this:
{
"items": [
{
"role": "MASTER",
"module": "USER_EDIT",
"permissions": [
"READ", "WRITE"
]
}
],
"total": 1
}
But I receive an exception:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:
java.lang.String cannot be cast to java.lang.Enum; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.lang.String cannot be cast to java.lang.Enum (through reference chain: com.myproject.dto.ListDto["items"]->java.util.ArrayList[0]->com.myproject.dto.PermissionDto["permissions"]->java.util.ArrayList[0])
...
Caused by: com.fasterxml.jackson.databind.JsonMappingException: java.lang.String cannot be cast to java.lang.Enum (through reference chain: com.myproject.dto.ListDto["items"]->java.util.ArrayList[0]->com.myproject.dto.PermissionDto["permissions"]->java.util.ArrayList[0])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:397)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:368)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:338)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContentsUsing(IndexedListSerializer.java:148)
...
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Enum
at com.fasterxml.jackson.databind.ser.std.EnumSerializer.serialize(EnumSerializer.java:27)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContentsUsing(IndexedListSerializer.java:142)
...
Api method:
@GetMapping
public ListDto<PermissionDto> permissions() {
return new ListDto<>(permissionsService.list(), permissionsService.count());
}
ListDto:
@Data
@AllArgsConstructor
public class ListDto<T> {
private List<T> items;
private long total;
}
PermissionDto:
@Data
public class PermissionDto {
private Role role;
private Module module;
private List<Permission> permissions;
}
Permission:
public enum Permission {
READ,
WRITE,
UPDATE,
DELETE;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|