jackson

ObjectMapper using TypeReference not working when passed type in generic method

≯℡__Kan透↙ 提交于 2021-02-07 20:53:14
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

ObjectMapper using TypeReference not working when passed type in generic method

怎甘沉沦 提交于 2021-02-07 20:53:04
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

ObjectMapper using TypeReference not working when passed type in generic method

雨燕双飞 提交于 2021-02-07 20:53:00
问题 This is the method: protected <T> TestPageResult<T> getTestPageResutForRequest(MockHttpServletRequestBuilder request) throws Exception { String responseJson = mockMvc.perform(request).andReturn().getResponse() .getContentAsString(); TestPageResult<T> response = getObjectMapper().readValue(responseJson, new TypeReference<TestPageResult<T>>() { }); return response; } I call it like this: TestPageResult<SomeDto> pageResult = this.<SomeDto>getTestPageResutForRequest(getRequest()); TestPageResult

How to convert Java class to Map<String, String> and convert non-string members to json using jackson?

孤街醉人 提交于 2021-02-07 20:12:44
问题 I have some class in Java that I want to convert to a Map<String, String> . The catch is that any fields of my java class that don't have an obvious String representation (collections, other classes) should be converted to json strings. Here's an example: @Data @AllArgsConstructor class MyClass { String field1; Long field2; Set<String> field3; OtherClass field4; } @Data @AllArgsConstructor class OtherClass { String field1; String field2; } ObjectMapper mapper = new ObjectMapper(); MyClass

JsonView not working correctly with Spring

醉酒当歌 提交于 2021-02-07 14:37:43
问题 I'm using Spring boot to create a restful api service. Everything works correctly, except that I cannot specify which data to be returned as JSON. What I want is to get the output without the field "content" ( which in my case is used for storing the object ). Spring version is 4.2.5 That's why I've defined 3 levels of views ( Internal being the one to be used when getting / setting object in the database, Public is the minimal data to be output to client ): public class EntityVisibility {

JsonView not working correctly with Spring

你说的曾经没有我的故事 提交于 2021-02-07 14:32:36
问题 I'm using Spring boot to create a restful api service. Everything works correctly, except that I cannot specify which data to be returned as JSON. What I want is to get the output without the field "content" ( which in my case is used for storing the object ). Spring version is 4.2.5 That's why I've defined 3 levels of views ( Internal being the one to be used when getting / setting object in the database, Public is the minimal data to be output to client ): public class EntityVisibility {

How to enable strict type parsing for jackson?

ε祈祈猫儿з 提交于 2021-02-07 12:52:15
问题 Jackson 1.9.9 is somewhat inconsistent in what it parses into scalar values (bool, int, string). Any array or object type fails but you can put any scalar type into a string. For bool 0 and not 0 are mapped to false/true. int attributes accept only numbers. public class Foo { public String s; public boolean b; public int i; } ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.readValue("{\"s\":\"abc\"}", Foo.class).s); // "abc" System.out.println(mapper.readValue("{\"s\":true

How to enable strict type parsing for jackson?

折月煮酒 提交于 2021-02-07 12:52:13
问题 Jackson 1.9.9 is somewhat inconsistent in what it parses into scalar values (bool, int, string). Any array or object type fails but you can put any scalar type into a string. For bool 0 and not 0 are mapped to false/true. int attributes accept only numbers. public class Foo { public String s; public boolean b; public int i; } ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.readValue("{\"s\":\"abc\"}", Foo.class).s); // "abc" System.out.println(mapper.readValue("{\"s\":true

How to make polymorphic jackson serialization working with map

谁说我不能喝 提交于 2021-02-07 10:24:36
问题 I try to have Jackson property type info serialized, even when my type is referenced by a map. With this simple sample: import java.util.HashMap; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class DemoJackson { @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) public static abstract

How to make polymorphic jackson serialization working with map

一世执手 提交于 2021-02-07 10:22:25
问题 I try to have Jackson property type info serialized, even when my type is referenced by a map. With this simple sample: import java.util.HashMap; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.annotation.JsonTypeName; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class DemoJackson { @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) public static abstract