Creating @JsonMixin annotation for spring controllers

I am using spring with jackson for json answers.

I want to create an annotation so that the jackson function is called mixins. The idea is similar to this question Using Jackson Mixins with MappingJacksonHttpMessageConverter and spring MVC

public @interface JsonMixin{ public class target(); public class mixin(); } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface JsonFilter{ public JsonMixin[] mixins(); } 

Using:

 @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); } 

Usually (without annotations) this is done as follows:

 @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); } 

Using this custom annotation, how would I snap to the mapper internal object and suggest using mixes provided by annotations?

+6
source share
1 answer

I ended up doing this guy:

https://github.com/martypitt/JsonViewExample

and created my own project here:

https://github.com/jackmatt2/JsonResponse

+8
source

All Articles