Example usage for org.springframework.data.rest.webmvc RestMediaTypes JSON_PATCH_JSON

List of usage examples for org.springframework.data.rest.webmvc RestMediaTypes JSON_PATCH_JSON

Introduction

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

Prototype

MediaType JSON_PATCH_JSON

To view the source code for org.springframework.data.rest.webmvc RestMediaTypes JSON_PATCH_JSON.

Click Source Link

Usage

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

/**
 * Returns all {@link JsonPatchOperation}s to be applied.
 * /* www  .ja v a  2s.c  o m*/
 * @param source must not be {@literal null}.
 * @return
 * @throws HttpMessageNotReadableException in case the payload can't be read.
 */
private Patch getPatchOperations(InputStream source) {

    try {
        return new JsonPatchPatchConverter().convert(mapper.readTree(source));
    } catch (Exception o_O) {
        throw new HttpMessageNotReadableException(
                String.format("Could not read PATCH operations! Expected %s!", RestMediaTypes.JSON_PATCH_JSON),
                o_O);
    }
}

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

/**
 * @see DATAREST-609/*from  w w  w.  jav a2  s.  c  o  m*/
 */
@Test
public void hintsToMediaTypeIfBodyCantBeRead() throws Exception {

    exception.expect(HttpMessageNotReadableException.class);
    exception.expectMessage(RestMediaTypes.JSON_PATCH_JSON.toString());

    handler.applyPatch(asStream("{ \"foo\" : \"bar\" }"), new User());
}

From source file:org.springframework.data.rest.tests.mongodb.MongoWebTests.java

@Test
public void testname2() throws Exception {

    Link usersLink = client.discoverUnique("users");
    Link userLink = assertHasContentLinkWithRel("self", client.request(usersLink));

    MockHttpServletResponse response = patchAndGet(userLink,
            "[{ \"op\": \"replace\", \"path\": \"/address/zipCode\", \"value\": \"ZIP\" },"
                    // + "{ \"op\": \"replace\", \"path\": \"/lastname\", \"value\": null }]", //
                    + "{ \"op\": \"remove\", \"path\": \"/lastname\" }]", //
            RestMediaTypes.JSON_PATCH_JSON);

    assertThat(JsonPath.read(response.getContentAsString(), "$.lastname"), is(nullValue()));
    assertThat(JsonPath.read(response.getContentAsString(), "$.address.zipCode"), is((Object) "ZIP"));
}

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

/**
 * The {@link HttpMessageConverter} used by Spring MVC to read and write JSON data.
 * // ww w. j  a va  2 s .  c  o m
 * @return
 */
@Bean
public TypeConstrainedMappingJackson2HttpMessageConverter jacksonHttpMessageConverter() {

    List<MediaType> mediaTypes = new ArrayList<MediaType>();

    // Configure this mapper to be used if HAL is not the default media type
    if (!config().useHalAsDefaultJsonMediaType()) {
        mediaTypes.add(MediaType.APPLICATION_JSON);
    }

    int order = config().useHalAsDefaultJsonMediaType() ? Ordered.LOWEST_PRECEDENCE - 1
            : Ordered.LOWEST_PRECEDENCE - 10;

    mediaTypes.addAll(Arrays.asList(RestMediaTypes.SCHEMA_JSON, //
            RestMediaTypes.JSON_PATCH_JSON, RestMediaTypes.MERGE_PATCH_JSON, //
            RestMediaTypes.SPRING_DATA_VERBOSE_JSON, RestMediaTypes.SPRING_DATA_COMPACT_JSON));

    TypeConstrainedMappingJackson2HttpMessageConverter jacksonConverter = new ResourceSupportHttpMessageConverter(
            order);
    jacksonConverter.setObjectMapper(objectMapper());
    jacksonConverter.setSupportedMediaTypes(mediaTypes);

    return jacksonConverter;
}