use of org.springframework.http.converter.json.MappingJacksonValue in project spring-framework by spring-projects.
the class RestTemplateIntegrationTests method jsonPostForObjectWithJacksonView.
@Test
public void jsonPostForObjectWithJacksonView() throws URISyntaxException {
HttpHeaders entityHeaders = new HttpHeaders();
entityHeaders.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8));
MySampleBean bean = new MySampleBean("with", "with", "without");
MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
jacksonValue.setSerializationView(MyJacksonView1.class);
HttpEntity<MappingJacksonValue> entity = new HttpEntity<>(jacksonValue, entityHeaders);
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class);
assertTrue(s.contains("\"with1\":\"with\""));
assertFalse(s.contains("\"with2\":\"with\""));
assertFalse(s.contains("\"without\":\"without\""));
}
use of org.springframework.http.converter.json.MappingJacksonValue in project spring-framework by spring-projects.
the class AbstractMappingJacksonResponseBodyAdvice method beforeBodyWrite.
@Override
public final Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType contentType, Class<? extends HttpMessageConverter<?>> converterType, ServerHttpRequest request, ServerHttpResponse response) {
MappingJacksonValue container = getOrCreateContainer(body);
beforeBodyWriteInternal(container, contentType, returnType, request, response);
return container;
}
use of org.springframework.http.converter.json.MappingJacksonValue in project spring-framework by spring-projects.
the class AbstractJackson2View method writeContent.
/**
* Write the actual JSON content to the stream.
* @param stream the output stream to use
* @param object the value to be rendered, as returned from {@link #filterModel}
* @throws IOException if writing failed
*/
protected void writeContent(OutputStream stream, Object object) throws IOException {
JsonGenerator generator = this.objectMapper.getFactory().createGenerator(stream, this.encoding);
writePrefix(generator, object);
Class<?> serializationView = null;
FilterProvider filters = null;
Object value = object;
if (value instanceof MappingJacksonValue) {
MappingJacksonValue container = (MappingJacksonValue) value;
value = container.getValue();
serializationView = container.getSerializationView();
filters = container.getFilters();
}
if (serializationView != null) {
this.objectMapper.writerWithView(serializationView).writeValue(generator, value);
} else if (filters != null) {
this.objectMapper.writer(filters).writeValue(generator, value);
} else {
this.objectMapper.writeValue(generator, value);
}
writeSuffix(generator, object);
generator.flush();
}
use of org.springframework.http.converter.json.MappingJacksonValue in project spring-framework by spring-projects.
the class AbstractJackson2View method filterAndWrapModel.
/**
* Filter and optionally wrap the model in {@link MappingJacksonValue} container.
* @param model the model, as passed on to {@link #renderMergedOutputModel}
* @param request current HTTP request
* @return the wrapped or unwrapped value to be rendered
*/
protected Object filterAndWrapModel(Map<String, Object> model, HttpServletRequest request) {
Object value = filterModel(model);
Class<?> serializationView = (Class<?>) model.get(JsonView.class.getName());
FilterProvider filters = (FilterProvider) model.get(FilterProvider.class.getName());
if (serializationView != null || filters != null) {
MappingJacksonValue container = new MappingJacksonValue(value);
container.setSerializationView(serializationView);
container.setFilters(filters);
value = container;
}
return value;
}
use of org.springframework.http.converter.json.MappingJacksonValue in project spring-framework by spring-projects.
the class MappingJackson2JsonView method filterAndWrapModel.
@Override
protected Object filterAndWrapModel(Map<String, Object> model, HttpServletRequest request) {
Object value = super.filterAndWrapModel(model, request);
String jsonpParameterValue = getJsonpParameterValue(request);
if (jsonpParameterValue != null) {
if (value instanceof MappingJacksonValue) {
((MappingJacksonValue) value).setJsonpFunction(jsonpParameterValue);
} else {
MappingJacksonValue container = new MappingJacksonValue(value);
container.setJsonpFunction(jsonpParameterValue);
value = container;
}
}
return value;
}
Aggregations