I want to create a ENUM which holds different statuses for possible database values and use them also to generate possible drop down statuses in FE:
import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; public enum BusinessCustomersStatus { A("active", "Active"), O("onboarding", "Onboarding"), NV("not_verified", "Not Verified"), V("verified", "Verified"), S("suspended", "Suspended"), I("inactive", "Inactive"); private String shortName; private String fullName; BusinessCustomersStatus(String shortName, String fullName) { this.shortName = shortName; this.fullName = fullName; } // Define the status field as the enum representation by using @JsonValue @JsonValue public String getShortName() { return shortName; } @JsonValue public String getFullName() { return fullName; } // Use the fromStatus method as @JsonCreator @JsonCreator public static BusinessCustomersStatus fromStatus(String statusText) { for (BusinessCustomersStatus status : values()) { if (status.getShortName().equalsIgnoreCase(statusText)) { return status; } } throw new UnsupportedOperationException(String.format("Unknown status: '%s'", statusText)); } }
Full code:
But when I make a POST request to add a new record I get this error:
21:36:52.033 [http-nio-8000-exec-1] DEBUG HttpEntityMethodProcessor[traceDebug:91] - Writing [org.merchant.dto.tickets.TicketsFullDTO@1c7e1a52] 21:36:52.034 [http-nio-8000-exec-1] WARN DefaultHandlerExceptionResolver[logException:207] - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: Problem with definition of [AnnotedClass org.merchant.database.service.tickets.TicketStatus]: Multiple 'as-value' properties defined ([method org.merchant.database.service.tickets.TicketStatus#getFullName()] vs [method org.merchant.database.service.tickets.TicketStatus#getShortName()]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Problem with definition of [AnnotedClass org.merchant.database.service.tickets.BusinessCustomersStatus]: Multiple 'as-value' properties defined ([method org.merchant.database.service.tickets.BusinessCustomersStatus#getFullName()] vs [method org.merchant.database.service.tickets.BusinessCustomersStatus#getShortName()]) (through reference chain: org.merchant.dto.tickets.TicketsFullDTO["status"])] 21:36:52.034 [http-nio-8000-exec-1] DEBUG OpenEntityManagerInViewInterceptor[afterCompletion:111] - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor 21:36:52.034 [http-nio-8000-exec-1] DEBUG DispatcherServlet[logResult:1131] - Completed 500 INTERNAL_SERVER_ERROR 21:36:52.035 [http-nio-8000-exec-1] DEBUG DispatcherServlet[traceDebug:91] - "ERROR" dispatch for POST "/api/error", parameters={} 21:36:52.035 [http-nio-8000-exec-1] DEBUG RequestMappingHandlerMapping[getHandler:522] - Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest) 21:36:52.035 [http-nio-8000-exec-1] DEBUG OpenEntityManagerInViewInterceptor[preHandle:86] - Opening JPA EntityManager in OpenEntityManagerInViewInterceptor 21:36:52.036 [http-nio-8000-exec-1] DEBUG HttpEntityMethodProcessor[writeWithMessageConverters:268] - Using 'application/json', given [application/json, text/plain, */*] and supported [application/json, application/*+json, application/json, application/*+json] 21:36:52.036 [http-nio-8000-exec-1] DEBUG HttpEntityMethodProcessor[traceDebug:91] - Writing [{timestamp=Wed Oct 27 21:36:52 UTC 2021, status=500, error=Internal Server Error, path=/api/manageme (truncated)...] 21:36:52.036 [http-nio-8000-exec-1] DEBUG OpenEntityManagerInViewInterceptor[afterCompletion:111] - Closing JPA EntityManager in OpenEntityManagerInViewInterceptor 21:36:52.037 [http-nio-8000-exec-1] DEBUG DispatcherServlet[logResult:1127] - Exiting from "ERROR" dispatch, status 500 Multiple 'as-value' properties defined ^C22:18:41.117 [SpringApplicationShutdownHook] DEBUG ApplicationAvailabilityBean[onApplicationEvent:77] - Application availability state ReadinessState changed from ACCEPTING_TRAFFIC to REFUSING_TRAFFIC
Note that I have changed TicketStatus with BusinessCustomersStatus into the error stack.
When I make a request I send this payload:
{"title":"ascascasc","description":"ascascasc","status":"assigned","submitDate":"2021-10-13T00:00:00.000Z","category":"service","severity":"normal"}
Do you know how I can solve this issue?
Advertisement
Answer
it may work if changed like this
public String getShortName() { return shortName; } @JsonValue public String getFullName() { return fullName; }