如何使用spring-boot在我的jackson反序列化程序中包含objeto root?
我尝试放入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
}
没有根。
答案 0 :(得分:3)
那是因为你在序列化而不是反序列化。尝试使用
var legend = svg.selectAll('.legend') // NEW
.data(pie(data)) // NEW
.enter() // NEW
.append('g') // NEW
.attr('class', 'legend') // NEW
.attr('transform', function(d, i) { // NEW
var height = legendRectSize + legendSpacing; // NEW
var offset = height * pie(data).length / 2; // NEW
var horz = -2 * legendRectSize; // NEW
var vert = i * height - offset; // NEW
return 'translate(' + horz + ',' + vert + ')'; // NEW
});
legend.append('rect') // NEW
.attr('width', legendRectSize) // NEW
.attr('height', legendRectSize) // NEW
.style('fill', function(d,i){return color(i);}) // NEW
.style('stroke', function(d,i){return color(i);}); // NEW
legend.append('text') // NEW
.attr('x', legendRectSize + legendSpacing) // NEW
.attr('y', legendRectSize - legendSpacing) // NEW
.text(function(d) { console.log("text: ");console.log(d);return d.data.key; });
答案 1 :(得分:0)
另一个选择是像这样用参数化通用root-wrapper类:
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.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);
}
}
如果您想使用同一个键进行多个响应换行,那就很有意义。