How to simply activate redis - spring-boot

I thought that for the design of a mvc project in spring boot it was possible to add later and simply redis to the project.
I have been trying for a few days to add redis to my project and I am having difficulties.
I added this in my pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
I added this in main
#Cacheable
and I have this error message:
Spring Data Redis - Could not safely identify store assignment for repository candidate interface *. If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository.
then following the explanation on stackoverflow I added:
#spring.data.redis.repositories.type=none
#spring.data.ldap.repositories.enabled=false
spring.main.allow-bean-definition-overriding=true
but I have this message:
Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost/<unresolved>:6379
So yes it can't connect but I thought that by adding #Cacheable everything would be configured automatically ?!
I am clearly lost I thought it was that simple!
Do you have a good link so that I can set up redis on my project? thank you
thank you so much

Related

Why is my H2 database Spring config not working?

I am always getting java.lang.NoSuchMethodError: org.springframework.orm.jpa.JpaTransactionManager$JpaTransactionObject.setReadOnly(Z)V
I do not know why.
I use H2 database in memory. According to spring there is no configuration necessary. I just include the dependency in my maven POM . But when I call the database it gives me this error.
public interface ClientRepository extends CrudRepository<Client, Long> {
I know a lot had this error before but I cannot see any mistake on my side. According to spring it should work out of the box. Just add maven dependency. Nothing more. So where is my error.
It seems that if you mix Spring boot dependencies and Spring framework dependencies maybe this could cause a problem. I really do not know. I removed this dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
It was not Spring boot . So I would not need it anyway. I think Spring boot will never ever need any Spring framework dependency I guess.
Also versions could maybe be an issue. I did not include any version. Because Spring boot should then just take the latest one. Right ? But now I have versions. Maybe this was also an issue.

Which version is compatible with which? Spring and JSTL

I am new to Maven. Now, in my POM, I declared I need Spring 3.0.5.RELEASE. After I started my server and tried to access a page, the server threw a java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config error.
Now after researching online, I found that I need to include a JSTL dependency like so:
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
I have faced this question multiple times. How do I know which version of jstl is comptabile with Spring 3.0.5.RELEASE
I know this might be a basic question but it will help everyone
You could also try generating a Spring Boot Maven project via http://start.spring.io/ rather then manually setting one up.

What exactly represent these dependency used to database access of a Spring project?

I am pretty new in JPA\Hibernate and I have the following doubt.
Into the pom.xml file of a Spring MVC projet I found the following dependency that are added to my project (that use JPA to implement my repository).
<dependencies>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.8.2.RELEASE</version>
</dependency>
</dependencies>
So I am trying to understand the architecture of this projet and what exactly does these dependendency.
So from what I know JPA is only a specification and I can't use it without an implementation. So is JPA something like a set of interface that specify what operation I can perform and then I need something that implement that operation?
1) So, from what I have understand, the first artifact (the hibernate-jpa-2.1-api) should be the JPA specification.
Here the structure documentation: https://docs.jboss.org/hibernate/jpa/2.1/api/ and infact looking into the listed package it seems to me that are almost interfaces but not implementation classes.
If this assertion is correct my doubt is: why if it is the JPA specification it is provided under hibernate that should be an implementation o this specification? (Exist different version of the JPA spcificication or is it unique?)
2) The hibernate-core artifact should be the JPA implementation (from what I know exist various implementation and Hibernate is one of these, another one is EclipseLink and so on). So this artifact provide me the tool that allows me to interact concretely with the database)
3) The spring-data-jpa is a part of the Spring Data that make easier create JPA repository (for example allow me to use the "Specification" concept to create query)
Is it my reasoning correct or am I missing something?
JPA is just annotations (kind of interfaces) you can use to mark your class Entities and relations. Later you can use Hibernate (or EclipseLink) as implementation of the JPA.
Thus you can change implementation leaving annotations (interfaces).
Check also this

Spring Boot: Do I have to add a jdbc pool dependency for Oracle connection?

First off I'm new to Spring Boot, so perhaps there is something simple I'm missing.
I have a small Spring Batch process that relies upon Spring Boot.
By default it uses the embedded H2 database.
I want it to use an oracle database.
So I set the url/username/password in the application.properties file
spring.datasource.url=jdbc:oracle:thin:#host:1521:batch
spring.datasource.username=username
spring.datasource.password=reallyCoolPassword
and add the dependecy to my Maven pom
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.client.version}</version>
</dependency>
I still get the embedded database. The only way I've been able to make it work is to add a dependency to a connection pool (for example tomcat).
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
Am I missing something? I was expecting Spring Boot to already have a dependency to tomcat that it would have brought in. Allowing me to override with dbcp or something else if needed.
hopefully someone can tell me what I've done wrong or help me straighten out my thinking.
In addition to providing url, name and password for DB you need to add spring.datasource.driverClassName.
We are using mysql as storage and we are adding this in properties:
spring.datasource.driverClassName: com.mysql.jdbc.Driver
Here is also link to properties for spring batch using oracle db.
Also you need to add your driver to project and oracle due to licence restrictions does not have public maven repository so you need to install driver. Here is useful link with walk through.

spring-boot-starter versus spring-boot-starter-xxx

I noticed that the Spring Boot Sample Data Redis declares the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
See here for full POM: https://github.com/spring-projects/spring-boot/blob/v1.0.0.RC4/spring-boot-samples/spring-boot-sample-data-redis/pom.xml
I see no mention of the <artifactId>spring-boot-starter-redis</artifactId>
My question is: when do I use spring-boot-starter versus spring-boot-starter-xxx where xxx is the name of the project (here Redis)?
The answer to the specific question: spring-boot-starter is a baseline for the others, and for standalone (non-web) apps that don't use any other Spring components - it has basic support for Spring, Logging, and Testing, but nothing else (no webapp features, no database etc.). Since all the other starters depend on it, once you use another one you can remove the vanilla starter. EDIT: see here https://github.com/spring-projects/spring-boot/commit/77fd127e09963a844f8fb4e574e1f0d9d3424d4e.
Up to you on the redis starter, but I would use the starter if it exists, since it will typically cut down on the number of dependencies you need to declare. The redis one actually doesn't add a lot of value (hence it didn't exist until recently), but it probably ought to be used in the sample.

Resources