是否有更好的方法(可能使用注释)将mixin添加到弹簧控制器。
目前我这样做:
@RequestMapping(value = "/accounts",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET,
params = "q")
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
public final String getAccountsViaQuery(@RequestParam("q") final String query)
throws JsonGenerationException, JsonMappingException, IOException {
final List<Account> matchingAccounts = accountService.findByAccountNameOrNumber(query);
ObjectMapper mapper = new ObjectMapper();
SerializationConfig serializationConfig = mapper.getSerializationConfig();
serializationConfig.addMixInAnnotations(Account.class, Account.SearchJsonMixin.class);
return mapper.writeValueAsString(matchingAccounts);
}
我宁愿这样做
@RequestMapping(value = "/accounts",
produces = MediaType.APPLICATION_JSON_VALUE,
method = RequestMethod.GET,
params = "q")
@ResponseBody
@ResponseStatus(value = HttpStatus.OK)
public final List<Account> getAccountsViaQuery(@RequestParam("q") final String query)
throws JsonGenerationException, JsonMappingException, IOException {
return accountService.findByAccountNameOrNumber(query);
}
对我来说看起来好多了,没有样板代码,返回类型是编译器检查的。
是否可以在我的控制器上使用某种注释来添加json mixin?