I have question regarding entities serialization to JSON using Spring MVC & Jackson. I have following entities:
@Table(name = «A»)
class A {
@Column(name = «Id»)
Integer id;
@Column(name = «Name»)
String name;
@JoinColumn(name = «B_Id»)
B b;
}
@Table(name = «B»)
class B {
@Column(name = «Id»)
Integer id;
@Column(name = «Name»)
String name;
}
and I have following REST controller which returns JSON response (using Jackson integration & Spring Data repository):
@RestController
public class AController {
@Inject
ARepository aRepository;
@RequestMapping(value = «index»)
List<A> getIndex() {
return aRepository.findAll();
}
}
I want to exclude from the response "name" fields (from A & B classes). So the desired JSON response should be like:
[{
id: 1,
b: {
id: 1
}
}]
I know that I can use @JsonView from Jackson library, but in this case (as far as I know) I need to put additional @JsonView annotations to the entity fields.
I want to keep my domain entities clean from UI related stuff and configure response JSON field list at the UI layer.
For example:
@JSON_ENTITY(fieldList = [‘id’, ‘b.id’]) //specify field list explicitly
//or
@JSON_ENTITY(view = MyJsonView.class) // that's also would be good
@RequestMapping(value = «index»)
List<A> getIndex() {
return aRepository.findAll();
}
Do you have any clues on how it can be done with Spring?