定义了多个 'as-value' 属性

Multiple 'as-value' properties defined

我想创建一个 ENUM,它保存可能的数据库值的不同状态,并使用它们在 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));
    }
}

完整代码:

https://github.com/rcbandit111/Search_specification_POC/blob/main/src/main/java/org/merchant/database/service/businesscustomers/BusinessCustomersStatus.java

但是当我发出 POST 添加新记录的请求时,我得到了这个错误:

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

请注意,我已将带有 BusinessCustomersStatus 的 TicketStatus 更改为错误堆栈。

当我发出请求时,我发送了这个负载:

{"title":"ascascasc","description":"ascascasc","status":"assigned","submitDate":"2021-10-13T00:00:00.000Z","category":"service","severity":"normal"}

你知道我怎么解决这个问题吗?

这样改可能会有效

public String getShortName() {
    return shortName;
}

@JsonValue
public String getFullName() {
    return fullName;
}