简体   繁体   English

将枚举转换为字符串时的奇怪行为

[英]Strange behavior when converting enum to string

I have an enum class SearchableField{name, version} and Query class我有一个枚举类 SearchableField{name, version} 和 Query 类

private Map<SearchableField,Object> query;
private Map<SearchableField, Long> field;
private Map<SearchableField,Long> sort;

I want to force the API body to make searches only on fields (name, version).我想强制 API 主体仅对字段(名称、版本)进行搜索。 The problem I found, sometimes it works fine I got the result I wanted, and sometimes without making any change to my code by restarting only the application suddenly it doesn't work anymore, and I got this error.我发现的问题,有时它运行良好,我得到了我想要的结果,有时通过仅重新启动应用程序而没有对我的代码进行任何更改,它突然不再工作了,我收到了这个错误。 after restarting the application without making any change neither on my code nor on the body JSON, it works well without getting errors.重新启动应用程序后,既没有对我的代码也没有对主体 JSON 进行任何更改,它运行良好而不会出错。

I tried to convert the map <enum, object> to map<String, object> but I got the same behavior我试图将地图 <enum, object> 转换为 map<String, object> 但我得到了相同的行为

        Map<String,Object> query = doc.getQuery().entrySet().stream()
                .collect(Collectors.toMap(e -> e.getKey().name(), Map.Entry::getValue));

So how can I avoid this problem, please?那么我怎样才能避免这个问题呢?


    "stack": "java.lang.ClassCastException: class java.lang.String cannot be cast to class com.example.SearchableField 
    (java.lang.String is in module java.base of loader 'bootstrap'; com.example.SearchableField is in unnamed module of loader 
    io.quarkus.bootstrap.classloading.QuarkusClassLoader @38a1c423)\r\n\tat com.example.model.Service.lambda$streamAllServices$0(Service.java:32)
    \r\n\tat java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Collectors.java:177)
    \r\n\tat java.base/java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
    \r\n\tat java.base/java.util.HashMap$EntrySpliterator.forEachRemaining(HashMap.java:1746)
    \r\n\tat java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    \r\n\tat java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    \r\n\tat java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
    \r\n\tat java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    \r\n\tat java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
    \r\n\tat com.example.model.Service.streamAllServices(Service.java:32)
    \r\n\tat com.example.rest.ServiceQueryCommand.getAllServices(ServiceQueryCommand.java:33)
    \r\n\tat com.example.rest.ServiceQueryCommand_Subclass.getAllServices$$superforward1(ServiceQueryCommand_Subclass.zig:126)
    \r\n\tat com.example.rest.ServiceQueryCommand_Subclass$$function$$1.apply(ServiceQueryCommand_Subclass$$function$$1.zig:33)
    \r\n\tat io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
    \r\n\tat io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:62)
    \r\n\tat io.quarkus.arc.runtime.devconsole.InvocationInterceptor.monitor(InvocationInterceptor.java:49)
    \r\n\tat io.quarkus.arc.runtime.devconsole.InvocationInterceptor_Bean.intercept(InvocationInterceptor_Bean.zig:516)
    \r\n\tat io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
    \r\n\tat io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
    \r\n\tat io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
    \r\n\tat com.example.rest.ServiceQueryCommand_Subclass.getAllServices(ServiceQueryCommand_Subclass.zig:207)
    \r\n\tat com.example.rest.ServiceQueryCommand$quarkusrestinvoker$getAllServices_1c4b93897207492bfe00d441fe9872b666e1a566.invoke
    (ServiceQueryCommand$quarkusrestinvoker$getAllServices_1c4b93897207492bfe00d441fe9872b666e1a566.zig:39)
    \r\n\tat org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
    \r\n\tat org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:7)
    \r\n\tat org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:141)
    \r\n\tat org.jboss.resteasy.reactive.server.vertx.VertxResteasyReactiveRequestContext$1$1.handle(VertxResteasyReactiveRequestContext.java:72)
    \r\n\tat org.jboss.resteasy.reactive.server.vertx.VertxResteasyReactiveRequestContext$1$1.handle(VertxResteasyReactiveRequestContext.java:69)
    \r\n\tat io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:100)
    \r\n\tat io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:63)
    \r\n\tat io.vertx.core.impl.EventLoopContext.lambda$runOnContext$0(EventLoopContext.java:38)
    \r\n\tat io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    \r\n\tat io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469)
    \r\n\tat io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    \r\n\tat io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986)
    \r\n\tat io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    \r\n\tat io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    \r\n\tat java.base/java.lang.Thread.run(Thread.java:829)"

Though the structure/data of Query class is not provided, I'll suggest modify the enum to have a String version/field.尽管未提供 Query 类的结构/数据,但我建议修改枚举以具有 String 版本/字段。 Something like就像是

enum SearchableField{
  name("NAME"), version("VERSION");

  private final String fieldName ; 
  
  SearchableField(final String fieldName){
    this.fieldName = fieldName;
  }
  
  public String getFieldName(){
    return this.fieldName;
  }
}

This way, you will have a well-predictable string version of the enum value you need.这样,您将拥有所需枚举值的可很好预测的字符串版本。 The name can be gotten from the getFieldName() method.该名称可以从 getFieldName() 方法中获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM