I'm new to Couchbase and Spring Data Couchbase and am having issues getting a simple find query to work. I am running the community edition of couchbase (version 6.6.0), have the latest version of Spring Data Couchbase, and have created a small spring boot app to query from a new bucket I created. I am able to connect to the database ok, but none of the find queries I am using return any data. I have gone into the bucket and added a few indexes, but that didn't seem to help either. I am hoping it is just something small that I am missing.
Here is my code...
Config
#Configuration
#EnableCouchbaseRepositories
public class Config extends AbstractCouchbaseConfiguration {
#Override
public String getConnectionString() {
return "couchbase://127.0.0.1";
}
#Override
public String getUserName() {
return "Administrator";
}
#Override
public String getPassword() {
return "admin1";
}
#Override
public String getBucketName() {
return "testing";
}
#Override
protected boolean autoIndexCreation() {
return true;
}
}
Model
#Document
public class UserInfo {
#Id
private String id;
#QueryIndexed
private String name;
public String getId() {
return id;
}
public String getName() {
return name;
}
}
Repository
#Repository
public interface UserRepository extends CrudRepository<UserInfo, String> {
List<UserInfo> findByName(String name);
}
Main code that runs on app startup. I never seem to be getting data back, even though I have a few documents in my bucket.
#Configuration
public class StartupConfig {
#Autowired
public TravelRepository travelRepository;
#Autowired
public UserRepository userRepository;
#PostConstruct
public void postConstruct() {
System.out.println("Processing data...");
List<UserInfo> userInfo = userRepository.findByName("Steve");
//List<NameInfo> nameInfo = nameRepository.findAll();
if (userInfo.size() == 0) {
System.out.println("No data found");
} else {
System.out.println("Data found: " + userInfo.get(0).getName());
}
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>couchbase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>couchbase</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-couchbase</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
OK, I figured out the issue. I was manually inserting data via the Admin console, and I didn't realize that you have to add an _class attribute to your documents. When Spring Data Couchbase generates its queries, they will look something like this:
Select * from testing where name = "Steve" and _class= "com.example.couchbase.model.UserInfo";
And it won't find any data if the _class attribute is missing (unless there is some property setting or something that lets you get around this).
Related
I am attempting to connect to a database using JDBC template and have a application.config file with the following:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/herosighting<br>
spring.datasource.username=root<br>
spring.datasource.password=<br>
I do know that this application.properties is being read as I tried changing the server.port and it did work.
Now I have the following code:
#Repository
public class LocationDaoImpl implements LocationDao
{
#Autowired
JdbcTemplate jdbc;
#Override
public Location addLocation(Location l) {
// TODO Auto-generated method stub
return null;
}
#Override
public void updateLocation(Location l) {
// TODO Auto-generated method stub
}
#Override
public List<Location> getAllLocations() {
// TODO Auto-generated method stub
return null;
}
#Override
public void deleteLocationById(int id) {
// TODO Auto-generated method stub
}
#Override
public List<Location> heroLocationsById(int heroId) {
// TODO Auto-generated method stub
return null;
}
#Override
public List<Location> sightingsOnDate(Date date) {
// TODO Auto-generated method stub
return null;
}
#Override
public Location getLocationById(int id)
{
final String SELECT_ROOM_BY_ID = "SELECT * FROM location WHERE id = ?";
return jdbc.queryForObject(SELECT_ROOM_BY_ID, new LocationMapper(), id);//returns room Object
}
}
The return in the last method is returning null becuase the JDBCTemplate is null.
I called the method in main to test it like so:
#Repository
public class Test {
#Autowired
static LocationDao dao;
public static void main(String[] args)
{
Location l = dao.getLocationById(1);
}
}
The interesting thing is, to my knowledge I had the exact same configuration in another file with exact same pom.xml and that project the jdbc worked
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sg</groupId>
<artifactId>SuperheroSightings</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SuperheroSightings</name>
<description>Full stack Spring application for Superhero Sightings</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
EDIT:As there was confusion with my question, I do have a main where i intalize the spring application and this works fine:
#SpringBootApplication
public class SuperheroSightingsApplication
{
public static void main(String[] args)
{
SpringApplication.run(SuperheroSightingsApplication.class, args);
}
}
I am not trying to run unit tests or anything. All I am trying to do is run a method from my DAO to see if the connection to the database is working. The issue is the JDBCTemplate is null and I cannot figure out why.
Here is the exact error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.sg.SuperheroSightings.Dao.LocationDao.getLocationById(int)" because "com.sg.SuperheroSightings.Test.dao" is null
at com.sg.SuperheroSightings.Test.main(Test.java:18)
This is not how a main method from a Spring Boot Application should look like.
It should be something like this to startup correctly:
public static void main(String[] args)
{
SpringApplication.run(Test.class, args);
}
I'm trying to create a hazelcast-spring application with listeners but with no success for now. When i try to autowire my UserListener is null.
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IQueue<Object> queue1 = hazelcastInstance.getQueue("q");
queue1.addItemListener(new UserListener(), true);
}
This is my main method and it's working ok when i add the item listener with new instance of UserListener class (my listener) but i need to use the spring autowired annotation because in the listener i have service->dao and i want everything to be autowired.Any ideas how can i accomplish this?
This is my whole code:
Main Class:
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IQueue<Object> queue1 = hazelcastInstance.getQueue("q");
queue1.addItemListener(new UserListener(), true);
}
Listener Class:
#Component
public class UserListener implements ItemListener<Object> {
#Autowired
private UserService userService;
public void itemAdded(ItemEvent<Object> arg0) {
if (arg0 != null) {
try {
userService.process(arg0);
} catch (SQLException e) {
e.printStackTrace();
}
} else {
System.out.println("null");
}
}
public void itemRemoved(ItemEvent<Object> item) {
System.out.println("The item is removed succesfully");
}
}
POM.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>hazelcastServer</groupId>
<artifactId>hazelcastServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.12.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
</project>
This part of your code
queue1.addItemListener(new UserListener(), true);
creates a new UserListener object bypassing Spring, so any fields annotated as #Autowired don't get instantiated.
You need to try
UserListener userListener = applicationContext.getBean(UserListener.class)
queue1.addItemListener(userListener, true);
It's probably worth checking if userListener object is null, as it might be the Spring component scanning hasn't found you class and created a #Bean, as component scanning can be selective on which packages it scans.
I have the following problem. I trying to start a Spring Boot application with the DB2 database with Hybernate. So i created a repository and used the #Autowired annotation to get some data from the DB. The problem is that when I run the application i receive the following error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field studenteRepository in com.ibm.snam.ai4legal.controller.HelloWorldController required a bean of type 'com.ibm.snam.ai4legal.repositories.StudenteRepository' that could not be found.
Action:
Consider defining a bean of type 'com.ibm.snam.ai4legal.repositories.StudenteRepository' in your configuration.
Here are the classes of the application
Application class:
package com.ibm.snam.ai4legal.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication(scanBasePackages = {"com.ibm"})
public class SBApplication {
public static void main(String[] args) {
SpringApplication.run(SBApplication.class, args);
}
}
Repository class:
package com.ibm.snam.ai4legal.repositories;
import org.springframework.data.repository.CrudRepository;
import com.ibm.snam.ai4legal.model.Studente;
public interface StudenteRepository extends CrudRepository<Studente, Integer>{
}
Model class:
package com.ibm.snam.ai4legal.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
#Entity
public class Studente{
#Id
#GeneratedValue
private int id;
private String nome;
private String cognome;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getnome() {
return nome;
}
public void setnome(String nome) {
this.nome = nome;
}
public String getcognome() {
return cognome;
}
public void setcognome(String cognome) {
this.cognome = cognome;
}
}
Controller class:
package com.ibm.snam.ai4legal.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.ibm.snam.ai4legal.repositories.StudenteRepository;
#RestController
public class HelloWorldController {
#Autowired
StudenteRepository studenteRepository;
#GetMapping(value = "/home")
public ModelAndView helloworld() {
ModelAndView hello = new ModelAndView("helloworld");
return hello;
}
}
and here the pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>projects</groupId>
<artifactId>springwebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.db2.jcc</groupId>
<artifactId>db2jcc4</artifactId>
<version>4.26.14</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<!-- <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>-->
<repositories>
<repository>
<id>repo</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<packaging>war</packaging>
</project>
On the internet I found that I should insert <context:annotation-config/> in some configuration file but I have no idea in which file I have to put it. Someone can help?
You have to use #ComponentScan annotation. Try the below code.
#ComponentScan({"com.ibm.*"})
#SpringBootApplication
public class SBApplication {
public static void main(String[] args) {
SpringApplication.run(SBApplication.class, args);
}
}
Also mention #Repository annotation in StudenteRepository class.
Either move SBApplication to com.ibm.snam.ai4legal package so it can benefit from default component scanning or add the following annotations to specify packages to be scanned for entities and repositories.
#SpringBootApplication(scanBasePackages = {"com.ibm"})
#EnableJpaRepositories(basePackages = {"com.ibm"})
#EntityScan(basePackages = {"com.ibm"})
public class SBApplication {
public static void main(String[] args) {
SpringApplication.run(SBApplication.class, args);
}
}
Since you are using spring-boot-starter-data-jpa you need to provide the annotation #EnableJpaRepositories to tell springboot to autoconfigure everything.So you might want to use the auto configuration feature of springboot.The #EnableJpaRepositories annotation is not mandatory for auto configuring the spring-data-jpa but in some cases if spring component scan didn't recognize spring-data-jpa in classpath you will have to use this annotation to tell spring to autoconfigure it.
#EnableJpaRepositories will enabling auto configuration support for Spring Data JPA required to know the path of the JPA the repositories. By default, it will scan only the main application package and its sub packages for detecting the JPA repositories.So take care to put the main application class at the root package of your application.
#EnableJpaRepositories(basePackages ="com.ibm")
#SpringBootApplication(scanBasePackages = {"com.ibm"})
public class SBApplication {
public static void main(String[] args) {
SpringApplication.run(SBApplication.class, args);
}
}
Also, if your entity classes are not in the same package then you can use the #EntityScan annotation to specify the base packages. In your case you have not specifies the #Repository annotation on your interface which will tell spring-boot to create default implementations for your interface.If that annotation is not provided then spring will just ignore the interface and the bean creation will not happen.You won't be able to autowire it .So provide that and have methods declared in your interface and spring-bot will take care of the rest.
#Repository
public interface StudenteRepository extends CrudRepository<Studente, Integer>{
//If to find a student record by the id attribute
public Studente findById();
}
I try to initialize my cache with data, when my application start, and that's not working.
My code:
springBootApplication
package com.r2b.springcache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan("com.r2b")
#SpringBootApplication
#EnableCaching
public class SpringCacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCacheApplication.class, args);
}
#Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("student");
}
}
student
package com.r2b.model;
public class Student {
String id;
String name;
String clz;
public Student(String id, String name, String clz) {
super();
this.id = id;
this.name = name;
this.clz = clz;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClz() {
return clz;
}
public void setClz(String clz) {
this.clz = clz;
}
//Setters and getters
}
studentService
package com.r2b.service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.r2b.model.Student;
#Service
public class StudentService
{
#Cacheable("student")
public Student getStudentByID(String id)
{
try
{
System.out.println("Going to sleep for 5 Secs.. to simulate backend call.");
Thread.sleep(1000*5);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return new Student(id,"Sajal" ,"V");
}
}
StudentController
package com.r2b.controller;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.r2b.model.Student;
import com.r2b.service.StudentService;
#RestController
public class StudentController
{
#Autowired
StudentService studentService;
#PostConstruct
public void init() {
studentService.getStudentByID("1");
}
#GetMapping("/student/{id}")
public Student findStudentById(#PathVariable String id)
{
System.out.println("Searching by ID : " + id);
return studentService.getStudentByID(id);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.r2b</groupId>
<artifactId>spring-cache</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-cache</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
when I go to http://localhost:8080/student/1 the first time, the cache is not active, and the response takes more than 5 seconds, but when I refresh, the cache responds, and the request takes a few milliseconds ! despite that I called the cache method in postConstruct, i have try with #AfterPropertiesSet and it not working too !
Any idea ?
Thank you
The answer is simple, but it took me almost a day to figure out methods decorated with #Cacheable have no effect in a #PostConstruct
Simply replace your #PostConstruct with a #EventListener(ApplicationReadyEvent.class)
#EventListener(ApplicationReadyEvent.class)
public void init() {
studentService.getStudentByID("1");
}
NB If an exception gets thrown from a method decorated with #PostConstruct or EventListener(ApplicationReadyEvent.class) event your application will exit...
This doesn't work because the proxy has not been initialized yet. This is actually documented in the user guide
In proxy mode (the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation (in effect, a method within the target object that calls another method of the target object) does not lead to actual caching at runtime even if the invoked method is marked with #Cacheable. Consider using the aspectj mode in this case. Also, the proxy must be fully initialized to provide the expected behavior, so you should not rely on this feature in your initialization code (that is, #PostConstruct).
This is exactly what you're doing here. Caching should be as transparent as possible so pre-loading the cache on startup looks a bit odd to me (and increase the startup time).
I'm using #ConfigurationProperties to keep track of my properties. Everything work fine, including class member for sub-configuration. Properties are well loaded and the framework uses the getter to determine the "name of the context". However I have trouble when trying validation, it does not validate sub properties.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.properties:
test.value = 4
test.value2 = 6
test.sub.subValue = 9
ValidationApplication.java
#SpringBootApplication
public class ValidationApplication implements CommandLineRunner
{
private final TestProperties properties;
public ValidationApplication(TestProperties properties) {
this.properties = properties;
}
#Override
public void run(String... args) {
System.out.println("=========================================");
System.out.println("Value: " + this.properties.getValue());
System.out.println("Value2: " + this.properties.getValue2());
System.out.println("SubValue: " + this.properties.getSub().getSubValue());
System.out.println("=========================================");
}
public static void main(String[] args) throws Exception {
new SpringApplicationBuilder(ValidationApplication.class).run(args);
}
}
Main ConfigurationProperties, TestProperties.java
#Component
#ConfigurationProperties(prefix = "test")
public class TestProperties
{
#Min(4)
private int value;
#Max(6)
private int value2;
private SubProperties sub;
... getters and setters ...
}
Sub ConfigurationProperties, SubProperties.java
#Component
#ConfigurationProperties
public class SubProperties
{
#Max(10)
private int subValue;
... getter & setter ...
}
So the output with the given properties file:
=========================================
Value: 4
Value2: 6
SubValue: 9
=========================================
If I go out of range with Value and Value2, all is fine (validation works):
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target be.test.TestProperties#6cb6decd failed:
Property: test.value2
Value: 7
Reason: must be less than or equal to 6
Property: test.value
Value: 2
Reason: must be greater than or equal to 4
Action:
Update your application's configuration
With these values however:
test.value = 4
test.value2 = 6
test.sub.subValue = 11
The validation does not trigger (even if there is a #Max constraint on sub property).
Is it normal?
What did I miss?
You missed a #Valid annotation:
#Component
#ConfigurationProperties(prefix = "test")
public class TestProperties
{
#Min(4)
private int value;
#Max(6)
private int value2;
#Valid
private SubProperties sub;
... getters and setters ...
}