我正在使用带杰克逊的春天来回应json。
我想创建一个注释,以允许jackson的一个名为mixins的功能。这个想法类似于这个问题Using Jackson Mixins with MappingJacksonHttpMessageConverter & Spring MVC
public @interface JsonMixin{
public class target();
public class mixin();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface JsonFilter{
public JsonMixin[] mixins();
}
用法:
@JsonFilter(mixins={
@JsonMixin(target=Target1.class, mixin=Mixin1.class),
@JsonMixin(target=Target2, mixin=Mixin2.class)
})
@RequestMapping(value = "/accounts/{id}",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET)
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
public final Account getAccountsViaQuery(@PathParam("id") final long id)
throws IOException {
return accountService.get(id);
}
通常(没有注释)它将按如下方式完成:
@RequestMapping(value = "/accounts/{id}",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET)
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
public final Account getAccountsViaQuery(@PathParam("id") final long id)
throws IOException {
final String matchingAccounts = accountService.findByAccountNameOrNumber(query);
ObjectMapper mapper = new ObjectMapper();
SerializationConfig serializationConfig = mapper.getSerializationConfig();
serializationConfig.addMixInAnnotations(Target1.class, Mixin1.class);
serializationConfig.addMixInAnnotations(Target2.class, Mixin2.class);
return mapper.writeValueAsString(matchingAccounts);
}
使用这个自定义注释我如何链接到内部对象映射器并告诉它使用注释提供的mixins?