Spring RestTemplate POST Request with URL encoded data

I think the problem is that when you try to send data to server didn’t set the content type header which should be one of the two: “application/json” or “application/x-www-form-urlencoded” . In your case is: “application/x-www-form-urlencoded” based on your sample params (name and color). This header means “what type of data my client sends to … Read more

Spring RestTemplate and generic types ParameterizedTypeReference collections like List

I worked around this using the following generic method: public <T> List<T> exchangeAsList(String uri, ParameterizedTypeReference<List<T>> responseType) { return restTemplate.exchange(uri, HttpMethod.GET, null, responseType).getBody(); } Then I could call: List<MyDto> dtoList = this.exchangeAsList(“http://my/url”, new ParameterizedTypeReference<List<MyDto>>() {}); This did burden my callers with having to specify the ParameterizedTypeReference when calling, but meant that I did not have to … Read more

Spring’s @RequestParam with Enum

If you are using Spring Boot, this is the reason that you should not use WebMvcConfigurationSupport. The best practice, you should implement interface org.springframework.core.convert.converter.Converter, and with annotation @Component. Then Spring Boot will auto load all Converter‘s bean. Spring Boot code @Component public class GenderEnumConverter implements Converter<String, GenderEnum> { @Override public GenderEnum convert(String value) { return … Read more

Overriding beans in Integration tests

Since Spring Boot 1.4.x there is an option to use @MockBean annotation to fake Spring beans. Reaction on comment: To keep context in cache do not use @DirtiesContext, but use @ContextConfiguration(name = “contextWithFakeBean”) and it will create separate context, while it will keep default context in cache. Spring will keep both (or how many contexts … Read more