@ResponseView(SummaryView.class)
@RequestMapping(value = "deneme", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<Employee> list(HttpSession session) {
return employeeBS.listAllWithDetail();
}
for the entity:
@Entity
@Table(name = "Employee")
public class Employee extends AbstractBaseModel {
@JsonView(SummaryView.class)
@Getter
@Setter
@Column(name = "FirstName")
private String firstName;
@JsonView(SummaryView.class)
@Getter
@Setter
@Column(name = "LastName")
private String lastName;
@JsonView(DetailView.class)
@Getter
@Setter
@Column(name = "Salary")
private int salary;
The key part is:
private ObjectMapper getMapperForView(final Class<?> view, final String filter, final String expand) {
ObjectMapper mapper = new ObjectMapper() {
private static final long serialVersionUID = -4051244861173830416L;
protected DefaultSerializerProvider _serializerProvider(SerializationConfig config) {
SerializationConfig withView = config.withView(view);
return super._serializerProvider(withView);
}
};
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
return mapper;
}
I want to implement fields filtering in Spring MVC. I have added filter parameter to the request and I have passed the necessary parameters to this java class:
/**
* Adds support for Jackson's JsonView on methods annotated with a
* {@link ResponseView} annotation
*
* @author martypitt
*
*/
public class ViewAwareJsonMessageConverter extends MappingJackson2HttpMessageConverter {
public ViewAwareJsonMessageConverter() {
super();
ObjectMapper defaultMapper = new ObjectMapper();
defaultMapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);
setObjectMapper(defaultMapper);
}
@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
if (object instanceof DataView && ((DataView) object).hasView()) {
writeView((DataView) object, outputMessage);
} else {
super.writeInternal(object, outputMessage);
}
}
protected void writeView(DataView view, HttpOutputMessage outputMessage) throws IOException,
HttpMessageNotWritableException {
JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
ObjectMapper mapper = getMapperForView(view.getView(), view.getFilter(), view.getExpand());
JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(outputMessage.getBody(), encoding);
try {
mapper.writeValue(jsonGenerator, view.getData());
} catch (IOException ex) {
throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
}
}
private ObjectMapper getMapperForView(final Class<?> view, final String filter, final String expand) {
ObjectMapper mapper = new ObjectMapper() {
private static final long serialVersionUID = -4051244861173830416L;
protected DefaultSerializerProvider _serializerProvider(SerializationConfig config) {
System.out.println("View:" + view + " Filter:" + filter + " Expand:" + expand);
SerializationConfig withView = config.withView(view);
if (fields != null && !fields.equals("")) {
SimpleBeanPropertyFilter propertyFilter = SimpleBeanPropertyFilter.filterOutAllExcept(fields
.split(","));
SimpleFilterProvider simpleFilterProvider = new SimpleFilterProvider().addFilter("myFilter",
propertyFilter);
withView = withView.withFilters(simpleFilterProvider);
}
return super._serializerProvider(withView);
}
};
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
return mapper;
}
}
The problem is when I give no filter parameter, now the default behavior with VIEW is no more working. It displays empty.
1. How I can achieve to filter fields after a specific VIEW is applied?
2. Also I wonder, why we have to use @JSonFilter annotation to the Employee class.
3. After completed field filtering part, I want to implement EXPAND part.