[英]Spring Boot ignoring Jackson Annotations in Rest Controller
So I have an abstract root class Model
which has various subclasses. 所以我有一个抽象的根类
Model
,它有各种子类。 Model is annotated with the following: 模型注释如下:
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "model")
@JsonSubTypes({
@JsonSubTypes.Type(value = LowerBoundThresholdModel.class, name = "LowerBoundThresholdAnomalyDetector"),
@JsonSubTypes.Type(value = UpperBoundThresholdModel.class, name = "UpperBoundThresholdAnomalyDetector"),
@JsonSubTypes.Type(value = MovingAverageLowerBoundThresholdModel.class, name = "MovingAverageLowerBoundThresholdAnomalyDetector"),
@JsonSubTypes.Type(value = MovingAverageUpperBoundThresholdModel.class, name = "MovingAverageUpperBoundThresholdAnomalyDetector"),
@JsonSubTypes.Type(value = WindowedUpperBoundThresholdModel.class, name = "WindowedLowerBoundThresholdAnomalyDetector"),
@JsonSubTypes.Type(value = WindowedUpperBoundThresholdModel.class, name = "WindowedUpperBoundThresholdAnomalyDetector")
})
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Data
public abstract class Model {
I have a controller annotated with @RestController
and a method like this: 我有一个用
@RestController
注释的控制器和一个这样的方法:
@GetMapping("/api/v1/models")
public ResponseEntity<Iterable<Model>> getAllModels() {
try {
return ResponseEntity.ok(modelService.getAll());
} catch (Exception e) {
return ResponseEntity.status(500).build();
}
}
I have one test which correctly serializes the Models: 我有一个测试正确序列化模型:
public class JacksonTest extends AbstractApplicationTest {
@Autowired
ObjectMapper mapper;
@Test
public void shouldSerialiseEvent() throws JsonProcessingException {
LowerBoundThresholdModel lowerBoundThresholdModel = new LowerBoundThresholdModel();
lowerBoundThresholdModel.setThreshold(1.0);
String s = mapper.writeValueAsString(lowerBoundThresholdModel);
assertThat(s).contains("model").contains("LowerBoundThresholdAnomalyDetector");
}
}
However when I test the actual RestController it doesn't seem to use the Jackson annotations and doesn't include the class information in the model
field as configured: 但是,当我测试实际的RestController时,它似乎没有使用Jackson注释,并且没有按照配置在
model
字段中包含类信息:
@Test
public void shouldReturnListOfExistingModels() throws Exception {
LowerBoundThresholdModel lowerBoundThresholdModel = new LowerBoundThresholdModel();
lowerBoundThresholdModel.setThreshold(1.0);
lowerBoundThresholdModelRepository.save(lowerBoundThresholdModel);
windowedLowerBoundThresholdModelRepository.save(windowedLowerBoundThresholdModel);
mockMvc.perform(get("/api/v1/models"))
.andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$[0].model", is("LowerBoundThresholdModel")))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].threshold", is(lowerBoundThresholdModel.getThreshold())))
}
but this test fails with no such JSON path $[0].model
但是这个测试失败了,
no such JSON path $[0].model
How can I make the controller use the same object mapper so that my JSON output from the controllers is correct? 如何让控制器使用相同的对象映射器,以便控制器的JSON输出正确?
EDIT: if I remove the line andExpect(MockMvcResultMatchers.jsonPath("$[0].model", is("LowerBoundThresholdModel")))
it passes so its not to do with Iterable
not being converted to an indexed array 编辑:如果我删除行和
andExpect(MockMvcResultMatchers.jsonPath("$[0].model", is("LowerBoundThresholdModel")))
它传递,所以它与Iterable
没有被转换为索引数组
Iterator
does not allow indexed access to a list of items. Iterator
不允许对项列表进行索引访问。 It just returns the next element. 它只返回下一个元素。 So calling
$[0]
fails. 所以调用
$[0]
失败了。
By widening the Interface to List
it works as expected. 通过扩展接口到
List
它按预期工作。
@GetMapping("/api/v1/models")
public ResponseEntity<List<Model>> getAllModels() {
try {
return ResponseEntity.ok(modelService.getAll());
} catch (Exception e) {
return ResponseEntity.status(500).build();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.