Problem in configuring spring boot and redis - spring-boot

I have an old spring boot application (1.5.0-FINAL) and I can't change this version.
I want to add redis to my application, that's what I did:
1) added the maven dep:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.10.RELEASE</version>
</dependency>
2) Added the property to my boot
#EnableCaching
public class MySpringBootApp{
3) Added config properties to check if it starts the connection:
spring.cache.type: redis
spring.redis.host: 192.168.99.100
spring.redis.port: 6379
The host/port above do not exist: I just want to see something like "connection error" on boot to make sure I configured everything but nothing appears! It seems that spring boot just doesn't try to use a cache.
Am I missing something?Maybe my spring boot version is too old?

Spring Boot parent pom already defines the versions of the starters, so remove the version from spring-boot-starter-data-redis dependency.
Your pom.xml would have at least these dependencies.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Next, #EnableCaching will look for beans with #Cacheable or #CachePut annotations.

Related

java.lang.NoClassDefFoundError: Could not initialize class org.springframework.web.reactive.function.client.DefaultExchangeStrategiesBuilder

I am trying to use spring WebClient wc = WebClient.create(); in a non-Spring application, but it looks like DefaultExchangeStrategiesBuilder.DEFAULT_EXCHANGE_STRATEGIES returns null
causing error:
java.lang.NoClassDefFoundError: Could not initialize class org.springframework.web.reactive.function.client.DefaultExchangeStrategiesBuilder
at org.springframework.web.reactive.function.client.ExchangeStrategies.withDefaults(ExchangeStrategies.java:67)
at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.initExchangeStrategies(DefaultWebClientBuilder.java:302)
at org.springframework.web.reactive.function.client.DefaultWebClientBuilder.build(DefaultWebClientBuilder.java:269)
at org.springframework.web.reactive.function.client.WebClient.create(WebClient.java:144)
I have added to my POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<version>2.6.5</version>
</dependency>
If your application is not a Spring Boot application and does not apply Spring Boot's dependency management, you should not use Spring Boot's starters.
Instead you should use Spring Framework's spring-webflux dependency directly, as well as a client library of your choice, like Reactor Netty, Jetty client, Apache HttpComponents...
You should also ensure that the Spring Framework BOM is applied to your project to avoid an incompatible mix of versions in your application:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>...</version>
<type>pom</type>
</dependency>

Autoconfiguation excluded but embedded servlet container is still being used

I've followed all the steps mentioned in the documentation to enable the traditional war deployment for spring boot app i.e excluded the EmbeddedServletContainerAutoConfiguration from the #EnableAutoConfiguration and I only have one such instance. Also extended SpringBootServletInitializer and make sure the packaged war doesnt have any tomcat starter dependency. Also ran the spring boot report and confirmed the EmbeddedServletContainerAutoConfiguration is in the exclusion list.
Apart from all the changes when I deploy the war it is still creating a embedded application context with embedded servlet contatiner.
What did I miss and what other areas can I inspect ? Spring boot version 1.5.13.
>
Hi Veeram,
We do not need to exclude the AutoConfiguration-classes, but excluding the tomcat dependency is needed.
You need to do is omit tomcat starter dependency from pom.xml. It gets pulled from spring-boot-starter-web as a transitive dependency. So, you need to add exclusion for it:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Now, as you are using <packaging>war</packaging> and using SpringBootServletInitializer, we would need servlet-api dependency on the classpath.
So, add the servlet dependency to your pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Hope this helps!

ClassNotFoundException with Spring WebFlux Security

I'm having the following problem:
I am trying to setup a basic Spring Boot (2.0.0.M2) Project containing Spring Webflux and Spring Security.
My pom.xml looks like this (generated via start.spring.io):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
...
</dependencies>
SpringSecurityConfig:
#EnableWebFluxSecurity
public class SecurityConfig extends WebFluxSecurityConfiguration {}
I am not getting any further because I get an exception on startup:
Caused by: java.lang.ClassNotFoundException: org.springframework.security.web.server.context.SecurityContextRepository
So I figure that the whole org.springframework.security.web.server package is not on the classpath, although it should be there, since it is included in the API docs.
Seems like I am doing something wrong, but I don't know what.
Currently Spring Boot's spring-boot-starter-security does not include spring-security-webflux.
Add it as project dependency explicitly.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-webflux</artifactId>
</dependency>
See this issue : Provide auto-configuration for WebFlux-based security.
BTW, check my working codes: https://github.com/hantsy/spring-reactive-sample/tree/master/boot
Updated: In Spring Boot 2.0.0.RELEASE, spring-boot-starter-security includes webflux support.

My Jersey Spring Boot 1.5.2 project doesn't recognize FormDataMultiPart

I tried to add FormDataMultiPart to my Jersey Spring Boot project. I am using Spring Boot 1.5.2 and the Spring Boot Jersey Starter.
I tried adding the full import org.glassfish.jersey.media.multipart.FormDataMultiPart, but it is not found.
It looks like spring-boot-starter-jersey Maven dependency does not include jersey-media-multipart. You need to add it to your pom.xml. On the other hand, Spring Boot does define a jersey.version property so you can use that to keep the versions in sync.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<!-- Multipart not included in Spring Boot. jersey.version defined by Spring Boot. -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
Once you add the jersey-media-multipart dependency to Maven, the FormDataMultiPart class can be found.

Spring Boot with Spring HATEOAS Maven conflicts

It seems when I add the dependency for the spring-hateoas
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
<version>0.13.0.RELEASE</version>
The Class below is no longer available on the classpath
org.springframework.web.bind.annotation.RestController;
I have tried to exclude various dependencies for the spring-hateoas but the APP no longer runs.
Has anyone had any luck running spring-hateoas within spring boot.
Absolutely no problem whatsoever. The #RestController annotation is still available and you shouldn't need to do any exclusion.
In case it helps, I'm currently using version 1.0.2 of Spring Boot:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.2.RELEASE</version>
</parent>
spring-boot-starter-web provides the #RestController:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
I don't define an explicit version for spring-hateoas in my pom.xml, but my build is pulling in 0.9.0.RELEASE:
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
As a side note, I can see from the Eclipse POM editor, that Spring HATEOAS is defining dependencies on Spring 3.2.7. However the spring-boot-starter-parent project manages the versions up to 4.0.3. Can you see what version of Spring you are getting? Have you perhaps not used the spring-boot-parent as a parent project?

Resources