Tag: jackson modules

使用Jackson将Java对象实例编写到YAML

我有一个’示例’Pojo类,如下所述。 任何一个tel都可以使用Jackson将Example类的实例保存到YAML文件中。 public class Example { String name; int value; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }

如何映射Mixins以嵌套JSON响应

我使用Jackson API将我的JSON响应映射到java对象 。 例如, 回复{name:’karthikeyan’,年龄:’24’,性别:’男’} @JsonProperty(“name”) public String _name; @JsonProperty(“age”) public int _age; @JsonProperty(“gender”) public String _gender; 是混合,它工作正常。(内部我们将映射这个pojo和混合)。 现在我如何在混合中表示以下响应? { name :’karthikeyan’, age:’24’, gender:’Male’, interest: { books:’xxx’, music:’yyy’, movie:’zzz’ } } 我试过以下,但没有运气。 @JsonProperty(“name”) public String _name; @JsonProperty(“age”) public int _age; @JsonProperty(“gender”) public String _gender; @JsonProperty(“interest”) public InterestPojo interestPojo; //created same format mix-in and pojo for […]

如何避免Joda对象中的内容字段?

我在文档中使用Joda对象(DateTime和DateTimeZone),每当我通过REST接口访问它时,我会得到包含这样的字段的条目 lastAggregationDate: { content: “2016-07-12T17:58:43.643Z” } 代替 lastAggregationDate: “2016-07-12T17:58:43.643Z” 我宣布了Joda Jackson依赖项,我看到这些类型的de / serializers,所以我很困惑这里有什么工作。 我在一个稍微修改过的Spring示例项目中重复了这种行为,但是使用了Java的本机日期类型而不是Joda的。 我已经将一个出生属性的日期添加到Person对象,并修改了shouldRetrieveEntity测试以查找$.dateOfBirth.content 。 我已经确认正在使用序列化程序,看起来LocalDate对象被视为资源而不是简单属性。

Jackson JSON使用多个参数构造函数进行反序列化

我已经在我的项目中使用了FasterXML / Jackson-Databind一段时间了,一切都很好,直到我发现这篇文章并开始使用这种方法来反序列化没有@JsonProperty注释的对象。 问题是,当我有一个带有多个参数的构造函数并使用@JsonCreator注释修饰此构造函数时,Jackson会抛出以下错误: Exception in thread “main” com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for com.eliti.model.Cruiser, annotations: {interface com.fasterxml.jackson.annotation.JsonCreator=@com.fasterxml.jackson.annotation.JsonCreator(mode=DEFAULT)}] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator at [Source: { “class” : “com.eliti.model.Cruiser”, “inventor” : “afoaisf”, “type” : “MeansTransport”, “capacity” : 123, “maxSpeed” : 100 }; line: 1, column: […]

@JsonCreator和mixin通过模块不适用于第三方类

我试图反序列化java.net.HttpCookie,它没有默认的no-arg构造函数并且得到: org.codehaus.jackson.map.JsonMappingException:没有为类型[simple type,class java.net找到合适的构造函数。 HttpCookie]:无法在[源码:java.io.StringReader@5a395674;来自JSON对象(需要添加/启用类型信息?)实例化。 line:1,column:35 这是使用jackson-mapper-asl v 1.9.13 我找到了没有默认构造函数的Jackson第三方类,并试图通过getDeserializationConfig和using模块使用他们的解决方案。 我在下面提供模块代码。 abstract class HttpCookieMixIn { @JsonCreator public HttpCookieMixIn(@JsonProperty(“name”) String name, @JsonProperty(“value”) String value) { logger.info(“Mixin called!”); } } public class MyModule extends SimpleModule { public MyModule() { super(“ModuleName”, new Version(0,0,1,null)); } @Override public void setupModule(SetupContext context) { context.setMixInAnnotations(java.net.HttpCookie.class, HttpCookieMixIn.class); logger.info(“Set mixin annotation”); } } 在服务器端点的构造函数中,我有以下内容: […]

从POJO生成Json Schema

是)我有的: 我正在从pojo生成JSON模式。 我生成模式的代码如下所示: ObjectMapper mapper = new ObjectMapper(); TitleSchemaFactoryWrapper visitor = new TitleSchemaFactoryWrapper(); mapper.acceptJsonFormatVisitor(clazz, visitor); JsonSchema schema = visitor.finalSchema(); schemas.put(clazz, mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema)); 我通过上面的代码生成了几个模式。 其中一个pojos有一个内部嵌入枚举来限制可能的值,如下所示: public class MyClass { @JsonProperty(“name”) private String name; @JsonProperty(“startDayOfWeek”) private MyClass.StartDayOfWeek startDayOfWeek; /** * The ID of a timezone returned by the timezones route. * */ @JsonProperty(“timezone”) private String timezone; @JsonIgnore private […]

将Jackson ObjectMapper类设置为不使用双重科学记数法

我正在为JsonSchema使用库com.fasterxml.jackson库,我正在创建一个IntegerSchema对象,当我使用下面的代码设置整数模式的范围时: main(){ IntegerSchema intSchema = new IntegerSchema(); // setMaximum accepts Double object intSchema.setMaximum(new Double(102000000)); // setMaximum accepts Double object intSchema.setMinimum(new Double(100)); printJsonSchema(intSchema); } public void printJsonSchema(JsonSchema schema){ ObjectMapper mapper = new ObjectMapper(); try { logger.info(mapper.writeValueAsString(schema)); } catch (JsonProcessingException e) { throw new IllegalStateException(e); } } 当我使用ObjectMapper将IntegerSchema转换为字符串时得到以下响应: {“type”:”integer”,”maximum”:1.02E8,”minimum”:100.0} 最大值和最小值将转换为科学计数法。 但是我需要非科学记谱法输出如下: {“type”:”integer”,”maximum”:102000000,”minimum”:100} 我无法更改IntegerSchema类。 请建议如何在不扩展IntegerSchema类的情况下获得所需的输出? 提前致谢