Allow to specify request body type in RestTemplate
This commit allows to specify the request body type in order to serialize generic types with a GenericHttpMessageConverter if needed. Issue: SPR-13154
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -19,11 +19,14 @@ package org.springframework.http;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.web.util.UriTemplate;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -148,4 +151,14 @@ public class RequestEntityTests {
|
||||
|
||||
}
|
||||
|
||||
@Test // SPR-13154
|
||||
public void types() throws URISyntaxException {
|
||||
URI url = new URI("http://example.com");
|
||||
List<String> body = Arrays.asList("foo", "bar");
|
||||
ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<String>>() {};
|
||||
|
||||
RequestEntity<?> entity = RequestEntity.post(url).body(body, typeReference.getType());
|
||||
assertEquals(typeReference.getType(), entity.getType());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -20,11 +20,16 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
@@ -32,6 +37,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.http.converter.json.MappingJacksonValue;
|
||||
@@ -215,7 +221,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
bean.setWith2("with");
|
||||
bean.setWithout("without");
|
||||
HttpEntity<MySampleBean> entity = new HttpEntity<MySampleBean>(bean, entityHeaders);
|
||||
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class, "post");
|
||||
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class);
|
||||
assertTrue(s.contains("\"with1\":\"with\""));
|
||||
assertTrue(s.contains("\"with2\":\"with\""));
|
||||
assertTrue(s.contains("\"without\":\"without\""));
|
||||
@@ -229,7 +235,7 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
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");
|
||||
String s = template.postForObject(baseUrl + "/jsonpost", entity, String.class);
|
||||
assertTrue(s.contains("\"with1\":\"with\""));
|
||||
assertFalse(s.contains("\"with2\":\"with\""));
|
||||
assertFalse(s.contains("\"without\":\"without\""));
|
||||
@@ -243,6 +249,21 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
assertEquals("Invalid content", helloWorld, s);
|
||||
}
|
||||
|
||||
@Test // SPR-13154
|
||||
public void jsonPostForObjectWithJacksonTypeInfoList() throws URISyntaxException {
|
||||
List<ParentClass> list = new ArrayList<>();
|
||||
list.add(new Foo("foo"));
|
||||
list.add(new Bar("bar"));
|
||||
ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<ParentClass>>() {};
|
||||
RequestEntity<List<ParentClass>> entity = RequestEntity
|
||||
.post(new URI(baseUrl + "/jsonpost"))
|
||||
.contentType(new MediaType("application", "json", Charset.forName("UTF-8")))
|
||||
.body(list, typeReference.getType());
|
||||
String content = template.exchange(entity, String.class).getBody();
|
||||
assertTrue(content.contains("\"type\":\"foo\""));
|
||||
assertTrue(content.contains("\"type\":\"bar\""));
|
||||
}
|
||||
|
||||
public interface MyJacksonView1 {};
|
||||
public interface MyJacksonView2 {};
|
||||
|
||||
@@ -290,4 +311,47 @@ public class RestTemplateIntegrationTests extends AbstractJettyServerTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
|
||||
public static class ParentClass {
|
||||
|
||||
private String parentProperty;
|
||||
|
||||
public ParentClass() {
|
||||
}
|
||||
|
||||
public ParentClass(String parentProperty) {
|
||||
this.parentProperty = parentProperty;
|
||||
}
|
||||
|
||||
public String getParentProperty() {
|
||||
return parentProperty;
|
||||
}
|
||||
|
||||
public void setParentProperty(String parentProperty) {
|
||||
this.parentProperty = parentProperty;
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeName("foo")
|
||||
public static class Foo extends ParentClass {
|
||||
|
||||
public Foo() {
|
||||
}
|
||||
|
||||
public Foo(String parentProperty) {
|
||||
super(parentProperty);
|
||||
}
|
||||
}
|
||||
|
||||
@JsonTypeName("bar")
|
||||
public static class Bar extends ParentClass {
|
||||
|
||||
public Bar() {
|
||||
}
|
||||
|
||||
public Bar(String parentProperty) {
|
||||
super(parentProperty);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -699,9 +699,9 @@ public class RestTemplateTests {
|
||||
given(requestFactory.createRequest(new URI("http://example.com"), HttpMethod.POST)).willReturn(this.request);
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
given(this.request.getHeaders()).willReturn(requestHeaders);
|
||||
given(converter.canWrite(String.class, null)).willReturn(true);
|
||||
given(converter.canWrite(String.class, String.class, null)).willReturn(true);
|
||||
String requestBody = "Hello World";
|
||||
converter.write(requestBody, null, this.request);
|
||||
converter.write(requestBody, String.class, null, this.request);
|
||||
given(this.request.execute()).willReturn(response);
|
||||
given(errorHandler.hasError(response)).willReturn(false);
|
||||
List<Integer> expected = Collections.singletonList(42);
|
||||
|
||||
Reference in New Issue
Block a user