jackson

@JsonSerialize and @JsonDeserialize not working when included in an annotation

旧巷老猫 提交于 2021-02-08 10:25:51
问题 I am trying to create a custom annotation to tokenize a given property always when it is annotated with, so I have the following structure: @JsonComponent public class TokenSerializer { @JsonSerialize(using = IdToTokenSerializer.class) // This does not work @JsonDeserialize(using = TokenToIdDeserializer.class) // This does not work @Retention(RetentionPolicy.RUNTIME) public static @interface TokenizedId { Class<?> value(); } public static class IdToTokenSerializer extends JsonSerializer<Long>

How to filter attributes from JSON response in spring?

十年热恋 提交于 2021-02-08 10:03:38
问题 I have a controller like the following, @RequestMapping(value = "rest/v1/tester") public class TestController { @RequestMapping(value = "/search", method = RequestMethod.GET) public ResponseEntity<SampleResults> search(@ModelAttribute("criteria")SampleCriteria criteria) throws Exception { SampleResults sampleResults = sampleService.search(criteria); return new ResponseEntity<>(sampleResults, OK); } } I have another controller like so, @RequestMapping(value = "rest/v1/second") public class

deserialize json using jackson with dynamic field name

北城以北 提交于 2021-02-08 08:21:19
问题 I'm trying to deserialize json using jackson, the problem is that field names are not always the same, for instance One call will give me { id: "blabla" aFieldname : { an object if type A} } Another call will give me { id: "blabla" anotherName : { the same kind of object } } I can't predict the name of the field. is it even possible ? 回答1: You could deserialize the JSON as a Map<String, Object> : ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> typeReference =

deserialize json using jackson with dynamic field name

别等时光非礼了梦想. 提交于 2021-02-08 08:18:15
问题 I'm trying to deserialize json using jackson, the problem is that field names are not always the same, for instance One call will give me { id: "blabla" aFieldname : { an object if type A} } Another call will give me { id: "blabla" anotherName : { the same kind of object } } I can't predict the name of the field. is it even possible ? 回答1: You could deserialize the JSON as a Map<String, Object> : ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> typeReference =

Convert JSON with duplicated keys with jackson

戏子无情 提交于 2021-02-08 06:56:28
问题 Im converting a json to a java object but I'm not getting what I want. I have duplicated keys "friend" in my json. My json: { "id" : "5ee2e2f780bc8e7511a65de9", "friends": [{ "friend": { "id": 1, "name": "Priscilla Lynch" }, "friend": { "id": 2, "name": "William Lawrence" } }] } Using readValue from ObjectMapper only takes the last one "friend" but I need both. I know JSONObject use Map to convert so that's why it is taking the last one. Result: Contacts(id=5ee2e2f780bc8e7511a65de9, friends=[

Jackson tries to use all argument constructor since update to 2.8.1

被刻印的时光 ゝ 提交于 2021-02-08 06:43:53
问题 I just updated spring boot from 1.3.2 to 1.4 and as a result also updated jackson from 2.6.5 to 2.8.1. With no other change in code jackson now tries to create instances of my model class using an all-args-constructor (AnnotatedConstructor.java:124) instead of the normal no-args-constructor (AnnotatedConstructor.java:120). Since jackson is using the constructor in the wrong order (setting a String for a Long) this results in an exception: Caused by: com.fasterxml.jackson.databind

Make Jackson Subtypes extensible without editing the Supertypes java-file

£可爱£侵袭症+ 提交于 2021-02-08 05:29:20
问题 In my company we have a fixed JSON message structure: { "headerVal1": "" "headerVal2": "" "customPayload": { "payloadType":"" } } I would like to have some kind of library, which allows me, to not care for the company defined message structure, and instead just send and receive the payload. My idea was, to define the structure of the company template as one object, and use subtypes of a PayloadObject . @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.MINIMAL_CLASS,

Is it possible to use a custom serializer/deserializer for a type by default in spring?

好久不见. 提交于 2021-02-08 05:25:32
问题 I have a type from a third party library ( JSONB from jooq) that I've written a custom serializer/deserializer for: @JsonComponent public class JSONBSerializer extends JsonSerializer<JSONB> { @Override public void serialize(JSONB jsonb, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(jsonb.toString()); } } @JsonComponent public class JSONBDeserializer extends JsonDeserializer<JSONB> { @Override public JSONB deserialize

Jackson ObjectMapper: readValue returns null

橙三吉。 提交于 2021-02-08 03:49:40
问题 I need to parse this json: { "id":"cpd4-b39c4b2a-b5cb-4581-b519-6034aaa7fe4e", "transactionId":"768a9be4-b5b3-452f-9bd3-9fff2e9ace5c", "status":"PUBLIC", "confidential":true, "expiringAt":1231231, "locked":true, "metadata":[ { "user":"admin", "creationTimestamp":1538578453285, "value":"metadata" } ], "security":"read", "timestampCreation":1538578453285, "userCreation":"admin", "appCreation":"app", "document":{ "id":null, "transactionId":"768a9be4-b5b3-452f-9bd3-9fff2e9ace5c", "docId":

Jackson ObjectMapper: readValue returns null

一世执手 提交于 2021-02-08 03:48:53
问题 I need to parse this json: { "id":"cpd4-b39c4b2a-b5cb-4581-b519-6034aaa7fe4e", "transactionId":"768a9be4-b5b3-452f-9bd3-9fff2e9ace5c", "status":"PUBLIC", "confidential":true, "expiringAt":1231231, "locked":true, "metadata":[ { "user":"admin", "creationTimestamp":1538578453285, "value":"metadata" } ], "security":"read", "timestampCreation":1538578453285, "userCreation":"admin", "appCreation":"app", "document":{ "id":null, "transactionId":"768a9be4-b5b3-452f-9bd3-9fff2e9ace5c", "docId":