My learning diary

Iterator to Stream

With Google Guava, you can transform an Iterator into a Stream with But that API is only available from Guava 21. If you don’t want to use Guava or if your Guava version is below 21, you need an additional step to convert your Iterator to a Stream: Stream<String> stream = Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED ), false ); Reference: mkyong.

Continue reading "Iterator to Stream"

No qualifying bean

In one of my previous posts, I avoided auto-configuring primitives. Otherwise, the No qualifying bean error will occur. Today, I couldn’t avoid that anymore. I had to auto-configure a boolean so that the consumer can enable or disable a feature in application.yml. So, how did I resolve this issue? I used @Value("{someKey:someDefault}") final boolean someFlag in the caller constructor. Before that, I tried attaching only @Bean to the getter, but it didn’t work out.

Continue reading "No qualifying bean"

Almost done with the custom starter

I was right. I forgot to enable the Mongo repository. And it wasn’t only that. I had two Mongo configuration classes. One in the consumer and another in the custom starter. I needed only one. I removed the configuration class from the custom starter. Then, I enabled the custom starter’s repository in the consumer’s configuration class. Below is an idea of how my configuration class looks like: @EnableMongoRepositories(basePackages = { "yourTLD.

Continue reading "Almost done with the custom starter"

Auto-configuring controllers and repositories

I auto-configured Spring Boot REST controllers by adding their names to the spring.factories file. On the other hand, I did not have much luck with auto-configuring Mongo collections (yet). I then ran the consumer’s codebase and used Postman to hit the newly-offered endpoints. Below is the response seen in Postman: { "error": { "IllegalArgumentException": "Couldn't find PersistentEntity for type class yourTLD.yourOrg.yourProject.somePackage.SomeModel!" } } I thought the Mongo repository was not auto-configured, so I went to check the consumer’s logs.

Continue reading "Auto-configuring controllers and repositories"

Spring Boot Auto-configuration

Every time I use Spring Security, I have to copy and paste a lot of boilerplate code. What if I made the boilerplate code configurable? I decided to venture into this idea. At first, I extracted only the method body which configures HttpSecurity. So I got myself something like a utility library. But this meant there was still some boilerplate code outside of the library. This approach required consumers to define their own configuration classes (annotated with @Configuration).

Continue reading "Spring Boot Auto-configuration"