本文整理汇总了Java中org.springframework.http.converter.json.MappingJacksonValue.setSerializationView方法的典型用法代码示例。如果您正苦于以下问题:Java MappingJacksonValue.setSerializationView方法的具体用法?Java MappingJacksonValue.setSerializationView怎么用?Java MappingJacksonValue.setSerializationView使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.http.converter.json.MappingJacksonValue
的用法示例。
在下文中一共展示了MappingJacksonValue.setSerializationView方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: beforeBodyWriteInternal
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
@Override
protected void beforeBodyWriteInternal(final MappingJacksonValue bodyContainer,
final MediaType contentType,
final MethodParameter returnType,
final ServerHttpRequest request,
final ServerHttpResponse response) {
final Mutable<Class<?>> viewClass = Mutable.of(View.Anonymous.class);
if (SecurityContextHolder.getContext().getAuthentication() != null && SecurityContextHolder.getContext().getAuthentication().getAuthorities() != null) {
final Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();
viewClass.mutateIf(View.User.class, authorities.stream().anyMatch(o -> o.getAuthority().equals("PRIV_USER")));
viewClass.mutateIf(View.Moderator.class, authorities.stream().anyMatch(o -> o.getAuthority().equals("PRIV_MODERATOR")));
viewClass.mutateIf(View.Admin.class, authorities.stream().anyMatch(o -> o.getAuthority().equals("PRIV_ADMIN")));
}
bodyContainer.setSerializationView(viewClass.get());
}
示例2: jsonView
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
@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>")));
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:18,代码来源:MappingJackson2XmlHttpMessageConverterTests.java
示例3: filterAndWrapModel
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
/**
* 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;
}
示例4: beforeBodyWriteInternal
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
@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]);
}
示例5: jsonPostForObjectWithJacksonView
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
@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\""));
}
示例6: getMethod1
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
/**
* GET request that sets a View based on the protocol version and then serializes the Order to JSON.
* @param id the ID of the Order
* @return a serializable representation of the view.
*/
@RequestMapping(value = "/method1/{id}", method = RequestMethod.GET)
public MappingJacksonValue getMethod1(@PathVariable int id) {
MappingJacksonValue result = new MappingJacksonValue(find(id));
result.setSerializationView(getOrderView());
return result;
}
示例7: getAllSeats
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
/**
* Get all Seats in the Seatmap.
*
* @param admin Boolean for admins to view full data.
*
* @return A list of all Seats in the seatmap.
*/
@PreAuthorize("isAuthenticated()")
@GetMapping
MappingJacksonValue getAllSeats(@RequestParam(value = "admin", required = false) boolean admin, @AuthenticationPrincipal User user) {
MappingJacksonValue result = new MappingJacksonValue(seatService.getAllSeats());
if (!admin || !user.getAuthorities().contains(Role.ROLE_ADMIN)) {
result.setSerializationView(View.Public.class);
}
return result;
}
示例8: getSeatGroupByName
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
/**
* Get all Seats in a group.
*
* @param group The name of the Seat group.
* @param admin Boolean for admins to view full data.
* @return A List of Seats in a group.
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/{group}")
MappingJacksonValue getSeatGroupByName(@PathVariable String group, @RequestParam(value = "admin", required = false) boolean admin, @AuthenticationPrincipal User user) {
MappingJacksonValue result = new MappingJacksonValue(seatService.getSeatGroupByName(group));
if (!admin || !user.getAuthorities().contains(Role.ROLE_ADMIN)) {
result.setSerializationView(View.Public.class);
}
return result;
}
示例9: getSeatByGroupAndNumber
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
/**
* Get a Seat based on seatGroup and seatNumber
*
* @param group Group of the Seat
* @param number Number in the group of the Seat
* @param admin Boolean for admins to view full data.
*
* @return The seat at the given location
*/
@PreAuthorize("isAuthenticated()")
@GetMapping("/{group}/{number}")
MappingJacksonValue getSeatByGroupAndNumber(@PathVariable String group, @PathVariable int number, @RequestParam(value = "admin", required = false) boolean admin, @AuthenticationPrincipal User user) {
MappingJacksonValue result = new MappingJacksonValue(seatService.getSeatBySeatGroupAndSeatNumber(group, number));
if (!admin || !user.getAuthorities().contains(Role.ROLE_ADMIN)) {
result.setSerializationView(View.Public.class);
}
return result;
}
示例10: createUpdateHttpEntity
import org.springframework.http.converter.json.MappingJacksonValue; //导入方法依赖的package包/类
private HttpEntity<MappingJacksonValue> createUpdateHttpEntity(final WarehouseS3Credentials s3Credentials) {
final MappingJacksonValue jacksonValue = new MappingJacksonValue(s3Credentials);
jacksonValue.setSerializationView(WarehouseS3Credentials.UpdateView.class);
return new HttpEntity<>(jacksonValue);
}