【问题标题】:Multiple 'as-value' properties defined定义了多个“as-value”属性
【发布时间】:2021-12-13 04:40:10
【问题描述】:

我想创建一个 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"}

你知道我该如何解决这个问题吗?

【问题讨论】:

  • @jccampanero 你能建议吗?
  • 澄清一下,你想要这个代码,例如:ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(System.out, BusinessCustomersStatus.A); 产生{"shortName": "active", "fullName": "Active"}?
  • 我需要把这段代码放在哪里?
  • 这不是您需要添加的代码,我只是想了解您希望 enum 在转换为 JSON 时的外观。
  • 当我得到 ENUM 数据时,我使用此代码将其转换为 DTO:Arrays.stream(TicketStatus.values()).map(g -> new TicketStatusTypesDTO(g.getShortName(), g.getFullName())).collect(Collectors.toList()); 这将产生:[{"code":"new","name":"New"},{"code":"assigned","name":"Assigned"},{"code":"in_progress","name":"In-progress"},{"code":"closed","name":"Closed"}] 到目前为止它工作正常。请查看帖子以查看我在发出 POST 请求时收到错误的有效负载。

标签: java spring spring-boot spring-rest


【解决方案1】:

如果像这样更改它可能会起作用

public String getShortName() {
    return shortName;
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 2013-08-31
    • 2018-04-04
    • 2018-09-14
    相关资源
    最近更新 更多