Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在springboot反序列化程序中包含带有jackson的根对象_Java_Jackson_Spring Boot_Json Deserialization - Fatal编程技术网

Java 在springboot反序列化程序中包含带有jackson的根对象

Java 在springboot反序列化程序中包含带有jackson的根对象,java,jackson,spring-boot,json-deserialization,Java,Jackson,Spring Boot,Json Deserialization,如何在我的带有spring boot的jackson反序列化程序中包含objeto根 我尝试输入application.properties spring.jackson.deserialization.UNWRAP_ROOT_VALUE=true 我尝试使用一个配置程序 @Configuration public class JacksonConfig { @Bean public Jackson2ObjectMapperBuilder jacksonBuilder() {

如何在我的带有spring boot的jackson反序列化程序中包含objeto根

我尝试输入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;
    }

}
我把注释放在课堂上

@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 nessa porra aqui",
  "dtResposta": null,
  "dsResposta": null,
  "cdUsuarioResposta": null,
  "nmUsuarioResposta": null
}

没有根。

这是因为您正在序列化而不是反序列化。试用

spring.jackson.serialization.WRAP_ROOT_VALUE=true

另一个选项是参数化通用根包装类,如下所示:

package com.example.wrappedResponse.model;

public class ResponseWrapper<T> {
    private T contato;

    public ResponseWrapper(T contato) {
        this.contato = contato;
    }

    public T getContato() {
        return response;
    }

    public void setContato(T contato) {
        this.contato = contato;
    }
}
package com.example.wrappedResponse.model;
公共类响应包装器{
私人T contato;
公共响应包装器(T contato){
this.contato=contato;
}
公共T getContato(){
返回响应;
}
公共无效setContato(T contato){
this.contato=contato;
}
}
然后在控制器中用该类型包装实体

package com.example.wrappedResponse.controller;

import com.example.wrappedResponse.model.EntityBase;    
import com.example.wrappedResponse.model.ResponseWrapper;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
class EntityWrappingController {

    @GetMapping("/get/wrapped/base/entity")
    public ResponseWrapper<EntityBase> status() {
        EntityBase entityToWrap;
        // get you entity from application …

        return new ResponseWrapper<>(entityToWrap);
    }
}
package com.example.wrappedResponse.controller;
导入com.example.wrappedResponse.model.EntityBase;
导入com.example.wrappedResponse.model.ResponseWrapper;
导入org.springframework.web.bind.annotation.RestController;
导入org.springframework.web.bind.annotation.GetMapping;
@RestController
类EntityWrappingController{
@GetMapping(“/get/wrapped/base/entity”)
公共响应包装状态(){
EntityBase EntityToRap;
//从应用程序获取实体…
返回新的ResponseWrapper(entityToWrap);
}
}

如果您想用同一个键包装多个响应,这是有道理的。

@OP您解决过这个问题吗?我现在也在为同样的问题挣扎。