How to solve Java List add – throwing java.lang.UnsupportedOperationException


You try to add an object to a list. But you get an Exception:

	at java.base/java.util.AbstractList.add(


List<SomeEvent> eventList = Arrays.asList(event);
//not working, because the list size is fixed


List<SomeEvent> eventList = new ArrayList<>(Arrays.asList(event));
//working, because the list size is NOT fixed

Of course, reading the Javadock of public static <T> List<T> asList(…) would have revealed it in advance:

 * Returns a fixed-size list backed by the specified array.


if else condition with Java Lamba – Howto

//lambda Predicate
private static Predicate<HttpStatus> isErrorCode = (HttpStatus statusCode) -> (statusCode.is5xxServerError() || statusCode.is4xxClientError());

//your method
public static Mono<ClientResponse> getResponse(ClientResponse clientResponse) {
        if (isErrorCode.test(clientResponse.statusCode())) {

Do not forget to checkout the BiPredicate as well 😉

Why Spring Webflux is better than HttpClient – HowTo in Java

A lot of Boilerplate Code can be avoided.

AND: it is reactive, non-blocking.

Example (really, an example) – the ‚old way‘:

        HttpPost request = new HttpPost("");
        request.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType());

        String json = someJsonConverter.toJson(someRequestDto);
        HttpEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);

        try {
            CloseableHttpResponse response2 = httpclient.execute(request);

            if (HttpStatus.SC_CREATED != response2.getStatusLine().getStatusCode()) {
                //TODO eventually handle http statuus additionally
                return returnHandleAndDoNotThrow();
            SomeResponseDto responseDto = (SomeResponseDto) 
            return Optional.ofNullable(responseDto);
        } catch (IOException e) {
            return Optional.empty();;

The new way:

        Function<UriBuilder, URI> function = uriBuilder ->  
                .body(Mono.just(someDtoInstance), SomeDto.class)
//any errors, status codes are handled by a filter which you can define as follows:


I am using Spring Boot for this, so I do have a Config class.

import ...
import org.springframework.context.annotation.Bean;
import reactor.core.publisher.Mono;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.context.annotation.Configuration;

public class WebClientConfig {

    //Create the Bean for Autowiring
    public WebClient webClient() {
        return WebClient.builder().filter(someStatusHandlingFilter()).build();
    //Handle status codes
    public static ExchangeFilterFunction someStatusHandlingFilter() {
        return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> {
            if ((clientResponse.statusCode().is4xxServerError() 
                || clientResponse.statusCode().is5xxClientError())) {
                return clientResponse                        
                        .flatMap(errorBody -> {
                            return Mono.error(someExceptionToLog);
            } else {
                return Mono.just(clientResponse);


            <version>your latest version goes here</version>
            <version>your latest version goes here</version>

Java Stream Custom Collector – Howto

Sometimes the commonly available Collectors are not what we need.
Here is one example for a Singleton Custom Collector:

public static <T> Collector<T, ?, T> singleton() {
    return Collectors.collectingAndThen(
            Collectors.toList(), list -> {
                if (list.size() != 1) {
                    // or log and return nil
                    throw new CustomException();
                return list.get(0);

//and use it:
        .filter(elem -> elem.getExampleAttribute() == 0).collect(singleton());

Howto generate a new SSH key pair

ssh-keygen -t ed25519 -C ""

Note that in this example we use an ed25519 key.

The -c is providing a comment to divide the key amongst others.

I alwas use the suggested path and just klick enter 😉

JUnit Test String contained / contains String

With the Hamcrest Matchers, do this:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.StringContains.containsString;

assertThat(actualData, containsString(expectedData));

Multiple Webapps with Docker Compose

  • Learn how to setup a custom network for your containers: container A talks to container B
  • Learn how to run multiple webapps inside Docker containers on different ports with Docker Compose

Download complete source code here

  • Create the network with $docker network create skynet
  • Run with $docker-compose up
  • Stop with $<Ctrl + C>
  • Clean up from time to time (beware, network will be deleted as well) with $docker system prune -a -f
  • access via http://localhost:8080/sample
  • access via http://localhost:8081/sample

How to test Websites with Java

A very good way of testing a website is Selenium:
The Groovy driven alternative is Tellurium which provides an easier entry but is not as good as Selenium for more complex Use Cases:

Code driven (JUnit-like):

  • Java Tests for Selenium
  • Selenium will start to interact with your WebBrowser, as guided by the Tests.
  • Bears strong resemblance to normal UNIT Tests


  • Create a Selenium Test Suite by recording your clicks and website navigation in your browser.
  • Replay in your browser