jackson

Dynamically add new subtypes after the original supertype has already been configured in jackson

為{幸葍}努か 提交于 2021-02-10 09:28:12
问题 I have one super class Field and there are two other classes that inherit the super class Field . I want to dynamically add subclass without affecting super class changes public class TestConfiguration { private List<Field> fields; } I want to use the mapping in this way when fields is instance of same class Field then without className property "Field" used for mapping { "fields" : [ { "name" : "First_name", "type" : { "fieldType" : { "name" : "string" } }, "required" : true }] } I want to

Serializing an empty class (with no field)

我怕爱的太早我们不能终老 提交于 2021-02-10 07:18:16
问题 I have the following class: class Foo { @JsonCreator public Foo() { } } And I get the following exception: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class Foo and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) It cannot be serialized in this way. And I don't want to ignore the value, I just want to see {} output as JSON. Any help will be appreciated. 回答1: You have to disable

Apache CXF default POST request body with Jackson

霸气de小男生 提交于 2021-02-10 06:51:55
问题 In an Apache CXF JAX-RS project I'm working on, I've configured the JSON provider to be Jackson. This generally works, but I'd like the POST request body to always be not null , so that if a client sends a request with an empty body (no JSON {} ), I'd still get a default POJO. E.g. CXF side: @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Path("/foo") public Response postFoo(FooObj foo) { if (foo == null) return Response.ok("No foo"); else return

Apache CXF default POST request body with Jackson

匆匆过客 提交于 2021-02-10 06:51:38
问题 In an Apache CXF JAX-RS project I'm working on, I've configured the JSON provider to be Jackson. This generally works, but I'd like the POST request body to always be not null , so that if a client sends a request with an empty body (no JSON {} ), I'd still get a default POJO. E.g. CXF side: @POST @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) @Path("/foo") public Response postFoo(FooObj foo) { if (foo == null) return Response.ok("No foo"); else return

Spring JsonDeserializer not working for the type String

我的未来我决定 提交于 2021-02-10 04:56:11
问题 I have a requirement to strip off all the special characters and control characters from the fields of type String in any of the Objects. Deserializer was registered but never executes during the runtime for Strings . I tried adding the same as an annotation @JsonDeserialize(using = StringProcessorComponent.class) , but the same issue. It works for any other type like Date / Long . Please let me know if I am missing any. Here is my Deserializer. @JsonComponent public class

How to serialise Enums as both Object Shape and default string?

穿精又带淫゛_ 提交于 2021-02-09 07:53:28
问题 For an enum with attributes, eg: public enum Thing { THING_A("a"), THING_B("b"); private String thing; private Thing(String thing) { this.thing = thing; } // Getters... } Jackson serializes as the name of the values, eg: mapper.writeValueAsString(Thing.THING_A)); // "THING_A" If we add the annotation to treat serialisation as an object: @JsonFormat(shape = JsonFormat.Shape.OBJECT) it will serialize the attributes: mapper.writeValueAsString(Thing.THING_A)); // "{"thing":"a"}" I'd like to be

Jackson global settings to deserialise array to custom list implementation

て烟熏妆下的殇ゞ 提交于 2021-02-08 21:42:23
问题 By default, Jackson is using java.util.ArrayList to deserialise a JSON array. Instead of this, I want to use a custom implementation. For example, Guava ImmutableList if value is present, or Collection.emptyList() if JSON array is empty or null. I want to configure this globally for ObjectMapper . Is there an easy way to do this? PS: My Jackson version is 2.9.7 回答1: General solution is to use custom module. You can define classes you would like to use for collections. For Guava there is a

Java : Json with duplicate keys to map using Jackson

天涯浪子 提交于 2021-02-08 15:43:51
问题 I have a json file with same key but different values as follows, { "domains" : { "A" : { "name" : "a", "type" : "a1" }, "B" :{ "name" : "r", "type" : "g1" }, "A" : { "name" : "b", "type" : "b1" } } } which is coming from external system . How to convert the json to java map object and access the different values of the key: A I am using something like below, map = mapper.readValue(json, new TypeReference<HashMap<String,String>>(){}); which returns a map with unique keys. But I need a map

How an optional property can be ignored when using jackson to convert Java object

僤鯓⒐⒋嵵緔 提交于 2021-02-08 12:28:39
问题 I'm using Jackson 1.9.2(org.codehaus.jackson) to convent from Java object to matching JSON construct. Here is my java object: Class ColorLight { String type; boolean isOn; String value; public String getType(){ return type; } public setType(String type) { this.type = type; } public boolean getIsOn(){ return isOn; } public setIsOn(boolean isOn) { this.isOn = isOn; } public String getValue(){ return value; } public setValue(String value) { this.value = value; } } If I did the following

Catching & Handling Jackson Exceptions with a custom message

♀尐吖头ヾ 提交于 2021-02-08 12:19:57
问题 I'm hoping to to catch some jackson exceptions that are occurring in a spring-boot API I am developing. For example, I have the following request class and I want to catch the error that occurs when the "questionnaireResponse" key in the JSON request object is null or blank i.e " " in the request object. @Validated @JsonRootName("questionnaireResponse") public class QuestionnaireResponse { @JsonProperty("identifier") @Valid private Identifier identifier = null; @JsonProperty("basedOn") @Valid