Search in sources :

Example 1 with JsonView

use of com.fasterxml.jackson.annotation.JsonView in project spring-framework by spring-projects.

the class MappingJackson2MessageConverter method getSerializationView.

/**
	 * Determine a Jackson serialization view based on the given conversion hint.
	 * @param conversionHint the conversion hint Object as passed into the
	 * converter for the current conversion attempt
	 * @return the serialization view class, or {@code null} if none
	 * @since 4.2
	 */
protected Class<?> getSerializationView(Object conversionHint) {
    if (conversionHint instanceof MethodParameter) {
        MethodParameter param = (MethodParameter) conversionHint;
        JsonView annotation = (param.getParameterIndex() >= 0 ? param.getParameterAnnotation(JsonView.class) : param.getMethodAnnotation(JsonView.class));
        if (annotation != null) {
            return extractViewClass(annotation, conversionHint);
        }
    } else if (conversionHint instanceof JsonView) {
        return extractViewClass((JsonView) conversionHint, conversionHint);
    } else if (conversionHint instanceof Class) {
        return (Class<?>) conversionHint;
    }
    // No JSON view specified...
    return null;
}
Also used : JsonView(com.fasterxml.jackson.annotation.JsonView) MethodParameter(org.springframework.core.MethodParameter)

Example 2 with JsonView

use of com.fasterxml.jackson.annotation.JsonView in project spring-framework by spring-projects.

the class Jackson2ServerHttpMessageReader method resolveReadHints.

@Override
protected Map<String, Object> resolveReadHints(ResolvableType streamType, ResolvableType elementType, ServerHttpRequest request) {
    Object source = streamType.getSource();
    MethodParameter parameter = (source instanceof MethodParameter ? (MethodParameter) source : null);
    if (parameter != null) {
        JsonView annotation = parameter.getParameterAnnotation(JsonView.class);
        if (annotation != null) {
            Class<?>[] classes = annotation.value();
            if (classes.length != 1) {
                throw new IllegalArgumentException("@JsonView only supported for read hints with exactly 1 class argument: " + parameter);
            }
            return Collections.singletonMap(AbstractJackson2Codec.JSON_VIEW_HINT, classes[0]);
        }
    }
    return Collections.emptyMap();
}
Also used : JsonView(com.fasterxml.jackson.annotation.JsonView) MethodParameter(org.springframework.core.MethodParameter)

Example 3 with JsonView

use of com.fasterxml.jackson.annotation.JsonView in project spring-framework by spring-projects.

the class JsonViewResponseBodyAdvice method beforeBodyWriteInternal.

@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]);
}
Also used : JsonView(com.fasterxml.jackson.annotation.JsonView)

Example 4 with JsonView

use of com.fasterxml.jackson.annotation.JsonView in project ocvn by devgateway.

the class OcdsController method ocdsPackages.

@ApiOperation(value = "Returns all available packages, filtered by the given criteria." + "This will contain the OCDS package information (metadata about publisher) plus the release itself.")
@RequestMapping(value = "/api/ocds/package/all", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json")
@JsonView(Views.Public.class)
public List<ReleasePackage> ocdsPackages(@ModelAttribute @Valid final YearFilterPagingRequest releaseRequest) {
    List<Release> ocdsReleases = ocdsReleases(releaseRequest);
    List<ReleasePackage> releasePackages = new ArrayList<>(ocdsReleases.size());
    for (Release release : ocdsReleases) {
        releasePackages.add(createReleasePackage(release));
    }
    return releasePackages;
}
Also used : ReleasePackage(org.devgateway.ocds.persistence.mongo.ReleasePackage) ArrayList(java.util.ArrayList) Release(org.devgateway.ocds.persistence.mongo.Release) ApiOperation(io.swagger.annotations.ApiOperation) JsonView(com.fasterxml.jackson.annotation.JsonView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with JsonView

use of com.fasterxml.jackson.annotation.JsonView in project spring-framework by spring-projects.

the class JsonViewRequestBodyAdvice method beforeBodyRead.

@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {
    JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class);
    Class<?>[] classes = annotation.value();
    if (classes.length != 1) {
        throw new IllegalArgumentException("@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);
    }
    return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);
}
Also used : MappingJacksonInputMessage(org.springframework.http.converter.json.MappingJacksonInputMessage) JsonView(com.fasterxml.jackson.annotation.JsonView)

Aggregations

JsonView (com.fasterxml.jackson.annotation.JsonView)12 ApiOperation (io.swagger.annotations.ApiOperation)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 DBObject (com.mongodb.DBObject)4 MethodParameter (org.springframework.core.MethodParameter)4 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)4 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)4 Release (org.devgateway.ocds.persistence.mongo.Release)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ReleasePackage (org.devgateway.ocds.persistence.mongo.ReleasePackage)1 PageRequest (org.springframework.data.domain.PageRequest)1 Pageable (org.springframework.data.domain.Pageable)1 MappingJacksonInputMessage (org.springframework.http.converter.json.MappingJacksonInputMessage)1