Tag: json deserialization

用spring反序列化JSON:未解析的前向引用Jackson Exception

我在Spring上使用API​​ Rest项目。 我有一个服务“CreateMateriel”,它作为参数数据JSON: 装备对象的JSON { “agence”: 1, “code”: “001”, “type”: “MyType” } “物质”与“Agence”有很多关系。 我把@JsonIdentityInfo标签用来使用Agence的Id而不是Agence的对象(看完这个话题后 ) @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = “idAgence”) @JsonIdentityReference(alwaysAsId = true) @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = “agence”) private Agence agence; 但是当我在POST / materiels上发送JSON时,我有这个例外: 2017-05-16 18:00:53.021 WARN 8080 — [nio-8080-exec-8] .wsmsDefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: […]

使用键作为值反序列化Jackson

我有一个类似于这样的JSON结构: “teams”: { “team1Id”: “team1Name”, “team2Id”: “team2Name” } 我想将它反序列化为这些Java类: class Teams { Team team1; Team team2; } class Team { String id; String name; } 正如您所见,team1Id和team2Id(它们是JSON密钥)应该转换为Java字段的值。 此外,第一个teamId / teamName对应该归因于存储在team1中的对象,而第二个对应存储在team2字段中。 是否有任何本地JACKSON映射器可以执行此操作,还是需要为此创建自己的自定义反序列化器?

在springboot反序列化器中包含带jackson的root对象

如何将我的jackson反序列化器中的objeto root包含在spring-boot中? 我尝试放入application.properties spring.jackson.deserialization.UNWRAP_ROOT_VALUE=true 我尝试使用一个配置器 @Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.featuresToEnable(DeserializationFeature.UNWRAP_ROOT_VALUE); builder.indentOutput(true).dateFormat(new SimpleDateFormat(“dd/MM/yyyy HH:mm:ss”)); builder.indentOutput(true); return builder; } } 我把注释放在我的classe中 @JsonRootName(“contato”) public class TbContato extends EntityBase { 但是没有工作我得到了这个回报: { “cdContato”: 12, “dtContato”: “03/08/2015 16:04:43”, “cdUsuario”: null, “nmParte”: “Fabio Ebner”, “nmEmailParte”: “fabioebner@gmail.com”, “nmAssunto”: “Assuntttoooo”, “dsMensagem”: “mensagem […]

如何根据json中的属性编写jackson反序列化器

我想在类Type上编写json反序列化器,这样当基于名称从给定的json反序列化Type时,它会根据某些工厂方法将值(类型为接口Being)映射到其当前实现,该工厂方法根据名称返回正确的类名,并填充剩余的类没有任何明确的反序列化,也没有明确地使用new创建TigerBeing或HumanBeing的对象。 我试图使用@jsonCreator,但我必须使用new初始化整个HumanBeing或TigerBeing并在构造函数中传递所有json。 我需要自动映射进一步用作进一步pojo的类型可能非常复杂。 {type:[{ “name”: “Human”, “value”: { “height”:6, “weight”:100, “languages”:[“spanish”,”english”] } }, { “name”:”Tiger”, “value”:{ “extinct”:1, “found”:[“Asia”, “America”, “Europe”, “Africa”] } } ]} I have: public class Type { String name; Being value; } public interface Being { } public class TigerBeing implements Being { Integer extinct; String[] found; } public class HumanBeing implement […]

jackson – @JsonTypeInfo属性被映射为null?

我正在处理这个问题。 假设我有这样的回答: { “id”:”decaa828741611e58bcffeff819cdc9f”, “statement”:”question statement”, “exercise_type”:”QUESTION” } 然后,基于exercise_type属性我想实例化不同的对象实例( ExerciseResponseDTO子类),所以我创建了这个混合: @JsonTypeInfo( use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = “exercise_type”) @JsonSubTypes({ @Type(value = ExerciseChoiceResponseDTO.class, name = “CHOICE”), @Type(value = ExerciseQuestionResponseDTO.class, name = “QUESTION”)}) public abstract class ExerciseMixIn {} public abstract class ExerciseResponseDTO { private String id; private String statement; @JsonProperty(value = “exercise_type”) private String exerciseType; […]

JSON:JsonMappingException尝试反序列化具有空值的对象

我尝试反序列化包含null属性的对象并具有JsonMappingException 。 我做的事: String actual = “{\”@class\” : \”PersonResponse\”,” + ” \”id\” : \”PersonResponse\”,” + ” \”result\” : \”Ok\”,” + ” \”message\” : \”Send new person object to the client\”,” + ” \”person\” : {” + ” \”id\” : 51,” + ” \”firstName\” : null}}”; ObjectMapper mapper = new ObjectMapper(); mapper.readValue(new StringReader(json), PersonResponse.class); //EXCEPTION! 但是 […]

Jackson ObjectMapper DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

我在Jersey应用程序中使用Jackson进行JSON序列化/反序列化。 我想在我的java POJO属性中将JSON中的空字符串读取为null值。 我试图在Object Mapper上设置DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT但它不起作用。 这是下面的代码 import java.io.IOException; import org.codehaus.jackson.JsonParseException; import org.codehaus.jackson.map.DeserializationConfig; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; public class TestMapper { /** * @param args */ public static void main(String[] args) { TestMapper.testJson(); } public static void testJson(){ String jsonString = “{\”name\”:\”First Name\”,\”phone\”:\”\”,\”unknown\”:\”test\”}”; ObjectMapper result = new ObjectMapper(); //result.setDeserializationConfig(result.getDeserializationConfig().with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)); //result.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); /*DeserializationConfig deserializeConfig = result.getDeserializationConfig(); deserializeConfig.with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);*/ result.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, […]