Search in sources :

Example 1 with MappingJacksonValue

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\""));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) MediaType(org.springframework.http.MediaType) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) Test(org.junit.Test)

Example 2 with MappingJacksonValue

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;
}
Also used : MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue)

Example 3 with MappingJacksonValue

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();
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 4 with MappingJacksonValue

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;
}
Also used : MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue) FilterProvider(com.fasterxml.jackson.databind.ser.FilterProvider)

Example 5 with MappingJacksonValue

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;
}
Also used : MappingJacksonValue(org.springframework.http.converter.json.MappingJacksonValue)

Aggregations

MappingJacksonValue (org.springframework.http.converter.json.MappingJacksonValue)6 FilterProvider (com.fasterxml.jackson.databind.ser.FilterProvider)2 Test (org.junit.Test)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MediaType (org.springframework.http.MediaType)1 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)1