#258 - Traverson now exposes default configuration per media types.
Introduced Traverson.getDefaultMessageConverters(…) to be able to obtain the list of default HttpMessageConverters depending on the given media types. Added overloads for varargs as well as a List for consistency.
This commit is contained in:
@@ -83,18 +83,47 @@ public class Traverson {
|
||||
* @param mediaType must not be {@literal null} or empty.
|
||||
*/
|
||||
public Traverson(URI baseUri, MediaType... mediaTypes) {
|
||||
this(baseUri, Arrays.asList(mediaTypes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Traverson} interacting with the given base URI and using the given {@link MediaType}s to
|
||||
* interact with the service.
|
||||
*
|
||||
* @param baseUri must not be {@literal null}.
|
||||
* @param mediaType must not be {@literal null} or empty.
|
||||
*/
|
||||
public Traverson(URI baseUri, List<MediaType> mediaTypes) {
|
||||
|
||||
Assert.notNull(baseUri, "Base URI must not be null!");
|
||||
Assert.notEmpty(mediaTypes, "At least one media type must be given!");
|
||||
|
||||
this.mediaTypes = Arrays.asList(mediaTypes);
|
||||
this.mediaTypes = mediaTypes;
|
||||
this.baseUri = baseUri;
|
||||
this.discoverers = DEFAULT_LINK_DISCOVERERS;
|
||||
|
||||
setRestOperations(createDefaultTemplate(this.mediaTypes));
|
||||
}
|
||||
|
||||
private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
|
||||
/**
|
||||
* Returns all {@link HttpMessageConverter}s that will be registered for the given {@link MediaType}s by default.
|
||||
*
|
||||
* @param mediaTypes must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static List<HttpMessageConverter<?>> getDefaultMessageConverters(MediaType... mediaTypes) {
|
||||
return getDefaultMessageConverters(Arrays.asList(mediaTypes));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all {@link HttpMessageConverter}s that will be registered for the given {@link MediaType}s by default.
|
||||
*
|
||||
* @param mediaTypes must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static List<HttpMessageConverter<?>> getDefaultMessageConverters(List<MediaType> mediaTypes) {
|
||||
|
||||
Assert.notNull(mediaTypes, "Media types must not be null!");
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
|
||||
@@ -103,8 +132,13 @@ public class Traverson {
|
||||
converters.add(getHalConverter());
|
||||
}
|
||||
|
||||
return converters;
|
||||
}
|
||||
|
||||
private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
|
||||
|
||||
RestTemplate template = new RestTemplate();
|
||||
template.setMessageConverters(converters);
|
||||
template.setMessageConverters(getDefaultMessageConverters(mediaTypes));
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2014 the original author or authors.
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,12 +16,14 @@
|
||||
package org.springframework.hateoas.client;
|
||||
|
||||
import static net.jadler.Jadler.*;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -38,6 +40,9 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.StringHttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
/**
|
||||
@@ -229,6 +234,32 @@ public class TraversonTests {
|
||||
assertThat(link.isTemplated(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #258
|
||||
*/
|
||||
@Test
|
||||
public void returnsDefaultMessageConvertersForHal() {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = Traverson.getDefaultMessageConverters(MediaTypes.HAL_JSON);
|
||||
|
||||
assertThat(converters, hasSize(2));
|
||||
assertThat(converters.get(0), is(instanceOf(StringHttpMessageConverter.class)));
|
||||
assertThat(converters.get(1), is(instanceOf(MappingJackson2HttpMessageConverter.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #258
|
||||
*/
|
||||
@Test
|
||||
public void returnsDefaultMessageConverters() {
|
||||
|
||||
List<HttpMessageConverter<?>> converters = Traverson.getDefaultMessageConverters(Collections
|
||||
.<MediaType> emptyList());
|
||||
|
||||
assertThat(converters, hasSize(1));
|
||||
assertThat(converters.get(0), is(instanceOf(StringHttpMessageConverter.class)));
|
||||
}
|
||||
|
||||
private void setUpActors() {
|
||||
|
||||
Resource<Actor> actor = new Resource<Actor>(new Actor("Keanu Reaves"));
|
||||
|
||||
Reference in New Issue
Block a user