Example usage for org.springframework.data.rest.webmvc IncomingRequest isPatchRequest

List of usage examples for org.springframework.data.rest.webmvc IncomingRequest isPatchRequest

Introduction

In this page you can find the example usage for org.springframework.data.rest.webmvc IncomingRequest isPatchRequest.

Prototype

public boolean isPatchRequest() 

Source Link

Document

Returns whether the request is a PATCH request.

Usage

From source file:org.springframework.data.rest.webmvc.config.JsonPatchHandler.java

/**
 * Applies the body of the given {@link IncomingRequest} as patch on the given target object.
 * /*from   ww w.  j  a v a  2 s .  com*/
 * @param request must not be {@literal null}.
 * @param target must not be {@literal null}.
 * @return
 * @throws Exception
 */
public <T> T apply(IncomingRequest request, T target) throws Exception {

    Assert.notNull(request, "Request must not be null!");
    Assert.isTrue(request.isPatchRequest(), "Cannot handle non-PATCH request!");
    Assert.notNull(target, "Target must not be null!");

    if (request.isJsonPatchRequest()) {
        return applyPatch(request.getBody(), target);
    } else {
        return applyMergePatch(request.getBody(), target);
    }
}

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

/**
 * Reads the given {@link ServerHttpRequest} into an object of the type of the given {@link RootResourceInformation},
 * potentially applying the content to an object of the given id.
 * //from w ww .  jav  a2s  .  c o m
 * @param information must not be {@literal null}.
 * @param request must not be {@literal null}.
 * @param converter must not be {@literal null}.
 * @param id must not be {@literal null}.
 * @return
 */
private Object read(RootResourceInformation information, IncomingRequest request,
        HttpMessageConverter<Object> converter, Object objectToUpdate) {

    // JSON + PATCH request
    if (request.isPatchRequest() && converter instanceof MappingJackson2HttpMessageConverter) {

        if (objectToUpdate == null) {
            throw new ResourceNotFoundException();
        }

        ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper();
        Object result = readPatch(request, mapper, objectToUpdate);

        return result;

        // JSON + PUT request
    } else if (converter instanceof MappingJackson2HttpMessageConverter) {

        ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper();

        return objectToUpdate == null ? read(request, converter, information)
                : readPutForUpdate(request, mapper, objectToUpdate);
    }

    // Catch all
    return read(request, converter, information);
}