Proper way of streaming using ResponseEntity and making sure the InputStream gets closed

you can try to use StreamingResponseBody StreamingResponseBody A controller method return value type for asynchronous request processing where the application can write directly to the response OutputStream without holding up the Servlet container thread. Because you are working on a separate thread, writing directly to the response, your problem to call close() before return is … Read more

How to prevent spring-boot autoconfiguration for spring-web?

For reference: This use case is documented in the Spring Boot Reference Guide: Not all Spring applications have to be web applications (or web services). If you want to execute some code in a main method, but also bootstrap a Spring application to set up the infrastructure to use, then it’s easy with the SpringApplication … Read more

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

tech