Java Code Examples for org.springframework.http.converter.json.MappingJacksonValue#setSerializationView()

The following examples show how to use org.springframework.http.converter.json.MappingJacksonValue#setSerializationView() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractJackson2View.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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);
		if (serializationView != null) {
			container.setSerializationView(serializationView);
		}
		if (filters != null) {
			container.setFilters(filters);
		}
		value = container;
	}
	return value;
}
 
Example 2
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void jsonView() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	JacksonViewBean bean = new JacksonViewBean();
	bean.setWithView1("with");
	bean.setWithView2("with");
	bean.setWithoutView("without");

	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView1.class);
	this.converter.write(jacksonValue, null, outputMessage);

	String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
	assertThat(result, containsString("<withView1>with</withView1>"));
	assertThat(result, not(containsString("<withView2>with</withView2>")));
	assertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
 
Example 3
Source File: AbstractJackson2View.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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);
		if (serializationView != null) {
			container.setSerializationView(serializationView);
		}
		if (filters != null) {
			container.setFilters(filters);
		}
		value = container;
	}
	return value;
}
 
Example 4
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void jsonView() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	JacksonViewBean bean = new JacksonViewBean();
	bean.setWithView1("with");
	bean.setWithView2("with");
	bean.setWithoutView("without");

	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView1.class);
	this.converter.write(jacksonValue, null, outputMessage);

	String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
	assertThat(result, containsString("<withView1>with</withView1>"));
	assertThat(result, not(containsString("<withView2>with</withView2>")));
	assertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
 
Example 5
Source File: MappingJackson2XmlHttpMessageConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void jsonView() throws Exception {
	MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
	JacksonViewBean bean = new JacksonViewBean();
	bean.setWithView1("with");
	bean.setWithView2("with");
	bean.setWithoutView("without");

	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView1.class);
	this.writeInternal(jacksonValue, outputMessage);

	String result = outputMessage.getBodyAsString(Charset.forName("UTF-8"));
	assertThat(result, containsString("<withView1>with</withView1>"));
	assertThat(result, not(containsString("<withView2>with</withView2>")));
	assertThat(result, not(containsString("<withoutView>without</withoutView>")));
}
 
Example 6
Source File: SecurityJsonViewControllerAdvice.java    From tutorials with MIT License 6 votes vote down vote up
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
                                       MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {
    if (SecurityContextHolder.getContext().getAuthentication() != null
            && SecurityContextHolder.getContext().getAuthentication().getAuthorities() != null) {
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        List<Class> jsonViews = authorities.stream()
                .map(GrantedAuthority::getAuthority)
                .map(AppConfig.Role::valueOf)
                .map(View.MAPPING::get)
                .collect(Collectors.toList());
        if (jsonViews.size() == 1) {
            bodyContainer.setSerializationView(jsonViews.get(0));
            return;
        }
        throw new IllegalArgumentException("Ambiguous @JsonView declaration for roles "+ authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.joining(",")));
    }
}
 
Example 7
Source File: JsonViewResponseBodyAdvice.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
		MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

	JsonView ann = returnType.getMethodAnnotation(JsonView.class);
	Assert.state(ann != null, "No JsonView annotation");

	Class<?>[] classes = ann.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
	}

	bodyContainer.setSerializationView(classes[0]);
}
 
Example 8
Source File: RestTemplateIntegrationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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\""));
}
 
Example 9
Source File: JsonViewResponseBodyAdvice.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
		MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

	JsonView ann = returnType.getMethodAnnotation(JsonView.class);
	Assert.state(ann != null, "No JsonView annotation");

	Class<?>[] classes = ann.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
	}

	bodyContainer.setSerializationView(classes[0]);
}
 
Example 10
Source File: RestTemplateIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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\""));
}
 
Example 11
Source File: AbstractJackson2View.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 12
Source File: JsonViewResponseBodyAdvice.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
		MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

	JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
	Class<?>[] classes = annotation.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
	}
	bodyContainer.setSerializationView(classes[0]);
}
 
Example 13
Source File: AbstractJackson2View.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 14
Source File: JsonViewResponseBodyAdvice.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType,
		MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) {

	JsonView annotation = returnType.getMethodAnnotation(JsonView.class);
	Class<?>[] classes = annotation.value();
	if (classes.length != 1) {
		throw new IllegalArgumentException(
				"@JsonView only supported for response body advice with exactly 1 class argument: " + returnType);
	}
	bodyContainer.setSerializationView(classes[0]);
}
 
Example 15
Source File: RestTemplateIntegrationTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void jsonPostForObjectWithJacksonView() throws URISyntaxException {
	HttpHeaders entityHeaders = new HttpHeaders();
	entityHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
	MySampleBean bean = new MySampleBean("with", "with", "without");
	MappingJacksonValue jacksonValue = new MappingJacksonValue(bean);
	jacksonValue.setSerializationView(MyJacksonView1.class);
	HttpEntity<MappingJacksonValue> entity = new HttpEntity<MappingJacksonValue>(jacksonValue, entityHeaders);
	String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
	assertTrue(s.contains("\"with1\":\"with\""));
	assertFalse(s.contains("\"with2\":\"with\""));
	assertFalse(s.contains("\"without\":\"without\""));
}