Hi, I am developing a micro service using spring boot.
as part of it, i came across an issue with Jackson mapper which i couldn't
resolve. any guidance is appreciated. please see the details below.
For example: i have following classes
import java.util.List;
public class School {
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
import java.util.List;
public class Student {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
In the spring boot class, i have following.
@SpringBootApplication
@RefreshScope
@RestController
public class Application
{
@RequestMapping("/")
public School welcome() {
School school= new School();
List<Student> students = new
ArrayList<Student>();
students.add(new Student());
school.setStudents(students);
return school;
}
@Configuration
class WebMvcConfiguration extends WebMvcConfigurationSupport {
@Override
protected void
extendMessageConverters(List<HttpMessageConverter<?>> converters) {
for(HttpMessageConverter<?> converter: converters) {
if(converter instanceof
MappingJackson2HttpMessageConverter) {
ObjectMapper mapper =
((MappingJackson2HttpMessageConverter)converter).getObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);
mapper.setSerializationInclusion(Include.NON_EMPTY);
}
}
}
}
}
the current output i get is :
{
"students": [
{}
]
}
Expected Output:
{}
since the students has the empty object in an array, i would like the
result to be ignored when serialized to JSON.
global solution did not work, as you can see i specified to ignore the
empty objects.
Don't add an empty student object to the list, could be suggested as a
solution but i would have to do for all the fields as the application grows.
Also i have scenarios where the Object in array can turn up empty, in turn
the array being empty, since the array has only one empty properties object.
can anyone suggest a solution that i can set globally to resolve this issue?
--
You received this message because you are subscribed to the Google Groups
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.