- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 jackson 2.3.3。我最近了解到 mixin annotations在 jackson ,我正在尝试将其应用到类里面。然而到目前为止还没有成功。有问题的类是一个静态类到另一个类:
public static class Report
{
// Some non relevant public static final fields, then:
public final int totalRuns;
public final int totalInvocations;
public final int totalMatches;
public final int totalMismatches;
public final double matchShare;
public final int reinvocations;
public final int rematches;
public final int remismatches;
public final double reinvocationShare;
public final long totalNanoTime;
public final List<RuleReport> ruleReports;
public Report(final int totalRuns, final int totalMatches,
final int totalMismatches, final int rematches,
final int remismatches, final long totalNanoTime,
final List<RuleReport> ruleReports)
{
this.totalRuns = totalRuns;
this.totalInvocations = totalMatches + totalMismatches;
this.totalMatches = totalMatches;
this.totalMismatches = totalMismatches;
this.matchShare = (double) totalMatches / (double) totalInvocations;
this.reinvocations = rematches + remismatches;
this.rematches = rematches;
this.remismatches = remismatches;
this.reinvocationShare = (double) reinvocations
/ (double) totalInvocations;
this.totalNanoTime = totalNanoTime;
this.ruleReports = ruleReports;
}
// Other, non relevant methods
}
并且没有其他构造函数。因此我这样写了我的 mixin 类:
public abstract class ProfilingReportMixin
{
@JsonProperty("runs")
private int totalRuns;
@JsonIgnore
private int totalInvocations;
@JsonProperty("matches")
private int totalMatches;
@JsonProperty("mismatches")
private int totalMismatches;
@JsonIgnore
private double matchShare;
@JsonIgnore
private int reinvocations;
@JsonProperty("rematches")
private int rematches;
@JsonProperty("remismatches")
private int remismatches;
@JsonIgnore
private double reinvocationShare;
@JsonIgnore
private long totalNanoTime;
@JsonProperty("ruleReports")
private List<RuleReport> ruleReports;
@JsonCreator
protected ProfilingReportMixin(
@JsonProperty("runs") final int totalRuns,
@JsonProperty("matches") final int totalMatches,
@JsonProperty("mismatches") final int totalMismatches,
@JsonProperty("rematches") final int rematches,
@JsonProperty("remismatches") final int remismatches,
@JsonProperty("totalNanoTime") final int totalNanoTime,
@JsonProperty("ruleReports") final List<RuleReport> ruleReports
)
{
}
}
由于没有“bean 构造函数”,显然我决定采用 @JsonCreator
方式。
我创建了 Module
,注册了它,没问题...除了在尝试反序列化示例 JSON 时它失败了:
Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class org.parboiled.parserunners.ProfilingParseRunner$Report]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: java.io.BufferedInputStream@b41b571; line: 2, column: 5]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1078)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:268)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2993)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2144)
at com.github.parboiled1.grappa.assertions.mixins.ProfilingReportMixin.main(ProfilingReportMixin.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
我不是很明白;我确实用 @JsonCreator
注释了 mixin 类的构造函数,到目前为止它一直对我有用,用于“正常”反序列化(这是我第一次尝试 mixin)。
这是 Jackson 的错误还是我做错了什么?
编辑:主要
:
public static void main(final String... args)
throws IOException
{
final ObjectMapper mapper = new ObjectMapper()
.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, true);
mapper.registerModule(GrappaModule.INSTANCE);
final Closer closer = Closer.create();
final InputStream in;
try {
in = closer.register(ProfilingReportMixin.class.
getResourceAsStream("/profilingReports/test.json"));
if (in == null)
throw new IOException("resource not found");
final ProfilingParseRunner.Report report
= mapper.readValue(in, ProfilingParseRunner.Report.class);
mapper.writerWithDefaultPrettyPrinter()
.writeValue(System.out, report);
} finally {
closer.close();
}
}
和模块
:
public final class GrappaModule
extends SimpleModule
{
private static final Version VERSION = new Version(1, 0, 0,
"beta.5-SNAPSHOT", "com.github.parboiled1", "grappa");
public static final Module INSTANCE = new GrappaModule();
private GrappaModule()
{
super("grappa", VERSION);
}
@Override
public void setupModule(final SetupContext context)
{
context.setMixInAnnotations(ProfilingParseRunner.RuleReport.class,
RuleReportMixin.class);
context.setMixInAnnotations(ProfilingParseRunner.Report.class,
ProfilingReportMixin.class);
}
}
最佳答案
确保您的 mixin 类上的构造函数签名与您的目标类上的构造函数签名相匹配。您的目标类构造函数将 totalNanoTime 作为 long,但您的混合构造函数将 totalNanoTime 作为 int。
░░░░░░░░░▄░░░░░░░░░░░░░░▄░░░░
░░░░░░░░▌▒█░░░░░░░░░░░▄▀▒▌░░░
░░░░░░░░▌▒▒█░░░░░░░░▄▀▒▒▒▐░░░
░░░░░░░▐▄▀▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐░░░
░░░░░▄▄▀▒░▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐░░░
░░░▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌░░░
░░▐▒▒▒▄▄▒▒▒▒░░░▒▒▒▒▒▒▒▀▄▒▒▌░░
░░▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐░░
░▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌░
░▌░▒▄██▄▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌░
▀▒▀▐▄█▄█▌▄░▀▒▒░░░░░░░░░░▒▒▒▐░
▐▒▒▐▀▐▀▒░▄▄▒▄▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▒▀▀▄▄▒▒▒▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▐░
░▌▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒░▒░▒░▒░▒▒▒▌░
░▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▄▒▒▐░░
░░▀▄▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▄▒▒▒▒▌░░
░░░░▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀░░░
░░░░░░▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀░░░░░
░░░░░░░░░▒▒▒▒▒▒▒▒▒▒▀▀░░░░░░░░
太神奇了。
关于java - Jackson 的 mixin 类不能解决问题 : bug or am I doing something wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23517120/
我找不到关于 jackson 的ObjectMapper与其他映射器(例如dozer/mapStruct/modelMapping/etc)之间的区别的任何解释。所有文章都比较了dozer/mapSt
我正在使用Jackson来反序列化Kotlin数据类。我正在使用jackson-kotlin-module,但 jackson 却给我以下错误: Can not construct instance
我正在尝试将包从“com.fasterxml.jackson”重新定位到我自己的包(即“mypackage.com.fasterxml.jackson”),然后在我拥有的另一个 JAR 中使用它。 我
对于JSON对象,Subject: { "permissions":["foo", "bar"], ... } ...我想反序列化为: class Subject { priv
我正在使用 @JsonTypeInfo 和 @JsonSubTypes 来映射基于给定属性的解析子类。这是我想要解析的示例 JSON 的一个人为示例。 { "animals": [ { "
我们正在使用 dropwizard 版本 0.6.3。当我们尝试升级 0.7.0 版本时,我们在服务启动时收到此错误。 线程“main”中的异常 java.lang.VerifyError: clas
我正在尝试实现自定义解串器。因为我只想向默认反序列化器添加功能,所以我尝试在我的自定义反序列化器中存储默认反序列化器:我想使用默认反序列化 json,然后添加其他信息。 我正在尝试使用 BeanDes
我有一个这样的类(class): public class Person { private String name; public String getName(){ return
我有以下 Kotlin 数据类: data class TestObject( val boolField: Boolean, val stringField: Str
使用 Jackson 库,在 Eclipse 4.9.0 版本中出现以下错误 缺少工件 com.fasterxml.jackson.core:jackson-databind:bundle:2.9.6
我试图在我的应用程序中从azure实现keyvault,在为DefaultAzureCredentialBuilder()实现azure-identity:1.5.4 lib后,它会抛出链接错误,如下
我试图在我的应用程序中从azure实现keyvault,在为DefaultAzureCredentialBuilder()实现azure-identity:1.5.4 lib后,它会抛出链接错误,如下
我知道我们可以使用 Jackson MixIn 来重命名属性或忽略属性(参见示例 here )。但是可以添加属性吗? 添加的属性可以是: 一个常数(如版本号) 计算值(例如,如果源类具有 getWid
我有一个在 Wildfly 10 上运行的应用程序,它需要更新版本的 jackson。简单地更新 maven 依赖是行不通的。 Wildflys 自己的版本似乎干扰了... 有人有提示吗? 最佳答案
我在 Tomcat 休息应用程序中运行 Jersey 2.5.1 & Jackson。对于我最初将 POJO 简单地转换为 JSON 的用例,基本设置效果很好。集合很好地转换为 json 数组,如下所
有没有办法告诉 jackson 在序列化过程中忽略那些用非 jackson 注释注释的字段? 例如: @SomeAnnotation private String foo; 我知道有 jackson
我遇到了 jackson 序列化问题, map 中存在空值。显然,这是 Wildfly 9 使用的 Jackson 版本中的一个已知错误 ( https://issues.jboss.org/brow
给定一个像这样的 JSON 映射类: public class Person { @JsonProperty String getName() { ... } @JsonPro
如何让 Jackson 的 XMLMapper 在反序列化时读取根 xml 元素的名称? 我正在将输入 XML 反序列化为通用 Java 类、LinkedHashMap,然后反序列化为 JSON。我想
我对抽象类和 JSON 序列化和反序列化的对象引用有问题。抽象的问题如下所示: 我有一个由节点和边组成的图。每条边连接两个节点。节点可以是红色和绿色的。因此,有一个抽象类Node和两个派生类 RedN
我是一名优秀的程序员,十分优秀!