Java Restlet将JSON转换为LinkedHashMap,而不是List<;MyObject>;?

Java Restlet将JSON转换为LinkedHashMap,而不是List<;MyObject>;?,java,json,restlet,restlet-2.0,Java,Json,Restlet,Restlet 2.0,这就是我的Rest端点的外观 @Post("json") public List<LogProcessorExpression> addLogProcessorExpression( final List<LogProcessorExpression> expressions) throws LPRestletException { if (expressions == null || expressions.isEmpty()) {

这就是我的
Rest
端点的外观

  @Post("json")
  public List<LogProcessorExpression> addLogProcessorExpression(
      final List<LogProcessorExpression> expressions) throws LPRestletException {
    if (expressions == null || expressions.isEmpty()) {
      return Collections.emptyList();
    }

    final Integer currentTenantId = Utils.getCurrentTenantId(getRequest());
    return customAttributesManager.addLogProcessorExpression(currentTenantId, expressions);
  }
List<LogProcessorExpression> addLogProcessorExpression(final Integer currentTenantId,
                                                       final List<LogProcessorExpression> expressions)
      throws LPRestletException {
    final Map<String, LogProcessorExpression> cache = getCacheByCustomAttributeName(expressions);
    try {
      final List<Customattributesmetadata> cams =
          getCustomAttributesMetaDataForTenant(currentTenantId);

      for (final Customattributesmetadata metadata : cams) {
        if (cache.containsKey(metadata.getAttributecolumnname())) {
          metadata.setLogprocessorexpression(
              cache.get(metadata.getAttributecolumnname()).toString());
        }
        metadata.save();
      }
    } catch (final TorqueException e) {
      final String error = "Failed to update LogExpression custom attributes";
      LOGGER.error(error, e);
      throw new LPRestletException(error, e);
    }

    return expressions;
  }
final Map<String, LogProcessorExpression> cache = getCacheByCustomAttributeName(expressions);  
return customAttributesManager.addLogProcessorExpression(currentTenantId, expressions);  
  Map<String, LogProcessorExpression> getCacheByCustomAttributeName(
      final List<LogProcessorExpression> expressions) {
    if (expressions == null || expressions.isEmpty()) {
      return Collections.emptyMap();
    }

    final Map<String, LogProcessorExpression> attributeByExpression = new HashMap<>();
    for (final LogProcessorExpression expression : expressions) {
      attributeByExpression.put(expression.getAttributeName(), expression);
    }
    return attributeByExpression;
  }
它回来了

{
   "code" : 500
   "message" : "The server encountered an unexpected condition which prevented it from fulfilling the request",
}
当我查看日志时,我看到的线条如下所示

        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.shn.api.dto.LogProcessorExpression
        at com.shn.api.restlet.logprocessor.CustomAttributesManager.getCacheByCustomAttributeName(CustomAttributesManager.java:55)
        at com.shn.api.restlet.logprocessor.CustomAttributesManager.addLogProcessorExpression(CustomAttributesManager.java:24)
        at com.shn.api.restlet.logprocessor.CustomAttributeMetadataRestlet.addLogProcessorExpression(CustomAttributeMetadataRestlet.java:44)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.restlet.resource.ServerResource.doHandle(ServerResource.java:521)
        ... 67 more
问题
-为什么不将它们强制转换到
列表中

-我需要做什么来修复它

更新

第24行看起来像

  @Post("json")
  public List<LogProcessorExpression> addLogProcessorExpression(
      final List<LogProcessorExpression> expressions) throws LPRestletException {
    if (expressions == null || expressions.isEmpty()) {
      return Collections.emptyList();
    }

    final Integer currentTenantId = Utils.getCurrentTenantId(getRequest());
    return customAttributesManager.addLogProcessorExpression(currentTenantId, expressions);
  }
List<LogProcessorExpression> addLogProcessorExpression(final Integer currentTenantId,
                                                       final List<LogProcessorExpression> expressions)
      throws LPRestletException {
    final Map<String, LogProcessorExpression> cache = getCacheByCustomAttributeName(expressions);
    try {
      final List<Customattributesmetadata> cams =
          getCustomAttributesMetaDataForTenant(currentTenantId);

      for (final Customattributesmetadata metadata : cams) {
        if (cache.containsKey(metadata.getAttributecolumnname())) {
          metadata.setLogprocessorexpression(
              cache.get(metadata.getAttributecolumnname()).toString());
        }
        metadata.save();
      }
    } catch (final TorqueException e) {
      final String error = "Failed to update LogExpression custom attributes";
      LOGGER.error(error, e);
      throw new LPRestletException(error, e);
    }

    return expressions;
  }
final Map<String, LogProcessorExpression> cache = getCacheByCustomAttributeName(expressions);  
return customAttributesManager.addLogProcessorExpression(currentTenantId, expressions);  
  Map<String, LogProcessorExpression> getCacheByCustomAttributeName(
      final List<LogProcessorExpression> expressions) {
    if (expressions == null || expressions.isEmpty()) {
      return Collections.emptyMap();
    }

    final Map<String, LogProcessorExpression> attributeByExpression = new HashMap<>();
    for (final LogProcessorExpression expression : expressions) {
      attributeByExpression.put(expression.getAttributeName(), expression);
    }
    return attributeByExpression;
  }
getCacheByCustomAttributeName(表达式)
看起来像

  @Post("json")
  public List<LogProcessorExpression> addLogProcessorExpression(
      final List<LogProcessorExpression> expressions) throws LPRestletException {
    if (expressions == null || expressions.isEmpty()) {
      return Collections.emptyList();
    }

    final Integer currentTenantId = Utils.getCurrentTenantId(getRequest());
    return customAttributesManager.addLogProcessorExpression(currentTenantId, expressions);
  }
List<LogProcessorExpression> addLogProcessorExpression(final Integer currentTenantId,
                                                       final List<LogProcessorExpression> expressions)
      throws LPRestletException {
    final Map<String, LogProcessorExpression> cache = getCacheByCustomAttributeName(expressions);
    try {
      final List<Customattributesmetadata> cams =
          getCustomAttributesMetaDataForTenant(currentTenantId);

      for (final Customattributesmetadata metadata : cams) {
        if (cache.containsKey(metadata.getAttributecolumnname())) {
          metadata.setLogprocessorexpression(
              cache.get(metadata.getAttributecolumnname()).toString());
        }
        metadata.save();
      }
    } catch (final TorqueException e) {
      final String error = "Failed to update LogExpression custom attributes";
      LOGGER.error(error, e);
      throw new LPRestletException(error, e);
    }

    return expressions;
  }
final Map<String, LogProcessorExpression> cache = getCacheByCustomAttributeName(expressions);  
return customAttributesManager.addLogProcessorExpression(currentTenantId, expressions);  
  Map<String, LogProcessorExpression> getCacheByCustomAttributeName(
      final List<LogProcessorExpression> expressions) {
    if (expressions == null || expressions.isEmpty()) {
      return Collections.emptyMap();
    }

    final Map<String, LogProcessorExpression> attributeByExpression = new HashMap<>();
    for (final LogProcessorExpression expression : expressions) {
      attributeByExpression.put(expression.getAttributeName(), expression);
    }
    return attributeByExpression;
  }
映射getCacheByCustomAttributeName(
最终列表(表达式){
if(expressions==null | | expressions.isEmpty()){
return Collections.emptyMap();
}
final Map attributeByExpression=new HashMap();
for(最终LogProcessorExpression表达式:表达式){
attributeByExpression.put(expression.getAttributeName(),expression);
}
返回attributeByExpression;
}

我认为restlet可能不够复杂,无法从处理程序方法的参数列表中提取参数化类型
List

@Post("json")
public List<LogProcessorExpression> addLogProcessorExpression(
  final List<LogProcessorExpression> expressions)
并使用该类型作为参数类型

@Post("json")
public List<LogProcessorExpression> addLogProcessorExpression(
  final LogProcessorExpressionList expressions)
@Post(“json”)
公共列表addLogProcessorExpression(
最终LogProcessorExpressionList表达式)

Jackson然后可以提取类型
LogProcessorExpressionList
的参数化超类型,即
ArrayList
,从中可以提取
LogProcessorExpression
作为目标元素类型。

也许您可以为此使用自定义Jackson反序列化程序吗?有关此功能,请参阅此链接:

这个答案为您提供了一些配置Jackson
ObjectMapper的提示:。这允许注册自定义反序列化程序,并参与反序列化处理,以便在列表中创建正确的对象

希望它能帮助你,
蒂埃里这是为我工作的

class LogProcessorExpressionList extends ArrayList<LogProcessorExpression> {}
类LogProcessorExpressionList扩展了ArrayList{}

刚刚更新的addLogProcessorExpression方法的返回类型是什么,感谢第24行和第44行完成了,您需要的一切我猜我的
DTO
需要实现
可序列化
,对吗?有趣的是,现在我看到
{“code”:422,“message”:“服务器理解请求实体的内容类型,请求实体的语法正确,但无法处理包含的指令”}
我需要使用getter/setter和空构造函数来
LogProcessorExpression
,这就解决了您的解决方案中的问题