Please refer to de-ignore Json fields in test cases for the requirement which I am working on.
As a solution to de-ignore Json fields in test cases, I am dynamically switching the json view using if else conditions as shown below.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
@Component
public class JacksonMapperUtil {
@Autowired
private MappingJackson2HttpMessageConverter jsonConverter;
public void modifyView(final Boolean internalView) {
if (internalView) {
this.jsonConverter.getObjectMapper().setConfig (
this.jsonConverter.getObjectMapper().getSerializationConfig().withView(View.Internal.class));
}
else {
this.jsonConverter.getObjectMapper().setConfig (
this.jsonConverter.getObjectMapper().getSerializationConfig().withView(View.Public.class));
}
}
}
But theoretically, when there are multiple requests at the same time where for one request Public view needs to be used and for other Internal view needs to be used, this should not work as we are updating the view in same object. So, I am looking for a solution where I can switch the view dynamically for each request. Please point me in the right direction.