lombok and #Valid not working together spring boot java - java

I am using lombok for generating Getter, Setter, AllArgsConstructor, NoArgsConstructor for User class and Doing some Validation with the help of #Valid annotation. But for some reason no Validation I put on User Class is working for me.
Below is the User Class
#Getter
#Setter
#AllArgsConstructor
#NoArgsConstructor
public class User {
#Size(min = 3, max = 15)
#NotBlank
private String username;
#NotNull
private String password;
}
Below is the Controller Class
#RestController
public class ValidationController {
#PostMapping("/validation")
public ResponseEntity<User> validationtest(#Valid
#RequestBody User user) {
return ResponseEntity.ok(user);
}
}
pom file is below
<?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.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.validation</groupId>
<artifactId>validation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>validation</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-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Import used
import javax.validation.Valid
import javax.validation.constraints.NotBlank;

Related

Spring does not see repository during scan on app start

Have sandbox springboot app. Decided to try reactive approach, postgres is used as database, I added r2dbc to the project to make my repository reactive. Here is my code:
#SpringBootApplication
public class Springboot2Application {
public static void main(String[] args) {
SpringApplication.run(Springboot2Application.class, args);
}
}
#Repository
public interface ToDoRepository extends
ReactiveCrudRepository<ToDo,String> {
}
#RestController
#RequestMapping("/api/v1")
public class ToDoController {
private final ToDoRepository repository;
public ToDoController(ToDoRepository repository) {
this.repository = repository;
}
#GetMapping(value = "/to-do", produces = {
MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE,
MediaType.TEXT_XML_VALUE})
public ResponseEntity<Flux<ToDo>> getToDos(#RequestHeader
HttpHeaders headers){
return ResponseEntity.ok().body(repository.findAll());
}
}
#Data
#RequiredArgsConstructor
#Entity
#Table(name = "todo")
public class ToDo {
#Id
#NotNull
#GeneratedValue(generator = "system-uuid")
#GenericGenerator(name = "system-uuid", strategy = "uuid")
private String id;
#NotNull
#NotBlank
private String description;
#CreationTimestamp
private Timestamp created;
#UpdateTimestamp
private Timestamp modified;
private boolean completed;
}
r2dbc config:
#Configuration
#EnableR2dbcRepositories(basePackages =
"com.springboot2.repository")
public class R2DBCConfig extends AbstractR2dbcConfiguration {
#Bean
public ConnectionFactory connectionFactory() {
return ConnectionFactories.get(
ConnectionFactoryOptions.builder()
.option(DRIVER, "postgresql")
.option(HOST, "localhost")
.option(PORT, 5432)
.option(USER, "admin")
.option(PASSWORD, "admin")
.option(DATABASE, "springdb")
.build());
}
}
On application start I'm getting:
Description:
Parameter 0 of constructor in com.springboot2.controller.ToDoController required a bean of type 'com.springboot2.repository.ToDoRepository' that could not be found.
Action:
Consider defining a bean of type 'com.springboot2.repository.ToDoRepository' in your configuration.
I tried to add #ComponentsScan, tried to move ToDoRepository to the root near Springboot2Application, I dont understand why Spring doesn't see repository interface
pom.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 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.7.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.springboot</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<!-- <spring-shell.version>2.1.0</spring-shell.version>-->
</properties>
<dependencies>
<!-- DB,ORM, and plugins-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>0.8.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<!-- Reactive libs-->
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
My guess the reason is that you have added r2dbc driver to your project, but haven't added spring-boot-starter-data-r2dbc
So, consider this dependency in your pom.xml file (this is the latest version at the time of writing this answer) :
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-r2dbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
<version>3.0.2</version>
</dependency>
EDIT:
Also you can try to use #EnableR2dbcRepositories and specify the package with your repository.
One more possible way to solve this is to extend your repository from R2dbcRepository, instead of ReactiveCrudRepository
This may help when you have multiple spring data reactive modules (r2dbc and something else) being used in the same project. Since ReactiveCrudRepository is a generic interface Spring does not know how to properly configure these beans.

Manager throws NullPointerException when using Togglz in Spring Boot application

I'm trying to use Togglz with Spring Boot 2.7.8.
I created this test application to get it up and running:
#SpringBootApplication
public class Testproject2Application {
#Autowired
private static FeatureManager manager;
public static final Feature TOGGLZ_TEST = new NamedFeature("TOGGLZ_TEST");
public static void main(String[] args) {
SpringApplication.run(Testproject2Application.class, args);
if (manager.isActive(TOGGLZ_TEST)) {
TogglzTest togglz = new TogglzTest(manager);
togglz.getTogglzStatus();
}
}
}
#Configurable
public class TogglzTest {
#Autowired
private FeatureManager manager;
#Autowired
public TogglzTest(FeatureManager manager) {
this.manager = manager;
}
public void getTogglzStatus(){
System.out.println("togglzOn is working");
}
}
My pom looks like this:
<?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.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.me</groupId>
<artifactId>testproject2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testproject2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.togglz</groupId>
<artifactId>togglz-spring-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I set these in application.properties:
togglz.enabled=true
togglz.features.TOGGLZ_TEST.enabled=true
When I run it I get:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.togglz.core.manager.FeatureManager.isActive(org.togglz.core.Feature)" because "com.paulcarron.testproject2.Testproject2Application.manager" is null
at com.paulcarron.testproject2.Testproject2Application.main(Testproject2Application.java:22)
I'm not sure what I should do with manager as nothing about this is mentioned in the Spring Boot Starter documentation.
What should I do to resolve this?

Spring Cloud Contract ContractVerifierTest.java not generated

So I'm trying to write contracts in Java, but the problem is that the verifying tests are not running at all. I've tried writing contracts in Groovy and it runs fine, I don't know what's the difference.
Is there some configuration I'm missing for the plugin? I'm following an example project from here.
This is the 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.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>contract_producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contract_producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
<spring-cloud.version>2020.0.5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<scope>test</scope>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-maven-plugin</artifactId>
<version>2.2.8.RELEASE</version>
<extensions>true</extensions>
<configuration>
<testFramework>JUNIT5</testFramework>
<baseClassForTests>
com.example.contract_producer.contracts.BaseTest
</baseClassForTests>
<contractsDirectory>src/test/java/contracts</contractsDirectory>
<depedencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-contract-verifier</artifactId>
<version>${spring-cloud-contract.version}</version>
<scope>compile</scope>
</dependency>
</depedencies>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
This is the contract written in Java
package contracts;
import org.springframework.cloud.contract.spec.Contract;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class UserControllerContracts implements Supplier<Contract> {
#Override
public Contract get() {
return Contract.make(new Consumer<Contract>() {
#Override
public void accept(Contract c) {
c.name("Get All User Contract");
c.description("Contract for /users and /user/all");
c.request(request -> {
request.method(request.GET());
request.url("/user/all");
});
c.response(response -> {
response.status(response.OK());
response.body("[{\"id\":1,\"email\":\"user#email.com\",\"password\":\"password\",\"name\":\"User\"}]");
});
}
});
}
public static class User {
Integer id;
String email;
String password;
String name;
public User(Integer id, String email, String password, String name) {
this.id = id;
this.email = email;
this.password = password;
this.name = name;
}
}
}
This is the contract written in Groovy
package contracts
import org.springframework.cloud.contract.spec.Contract
Contract.make {
description 'Should return all user in database'
request {
method GET()
url '/user/all'
}
response {
status OK()
headers {
contentType applicationJson()
}
body '''
[{
"id": 1,
"email": "user#email.com",
"password": "password",
"name": "User"
}]
'''
}
}
You're using wrong version of the plugin. Look that you're using contract in version 3.0.x through the 2020.0.x release train plugin. Since the old plugin generates junit4 tests the latest boot doesn't run it. Just upgrade the plugin to the latest 3.0.x version.

Spring Unit Test works in IntelliJ but not when using Maven (Multi Project)

So, I have the following setup:
Project
Module Database
Module Core
Module REST
Core and REST have Database as a Dependency and inside Database is a Configuration Class which connects to the Database on Startup.
Now, when i run each test of this class (default test looks like this:
#RunWith(SpringRunner.class)
#SpringBootTest
public class RestfulApplicationTests {
#Test
public void contextLoads(){}
}
It works when directly ran from IntelliJ.
BUT if i do "clean install" in Maven, Maven cannot find the dependencys. Example pom.xml from my Core Project
<?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>myproject</groupId>
<artifactId>myproject-restful</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>myproject-restful</name>
<description>Restful Services for myproject</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>myproject</groupId>
<artifactId>backend-cassandra</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>myproject</groupId>
<artifactId>backend-cassandra</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Pom from the DB Part:
<?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>de.mycompany.mypackage</groupId>
<artifactId>backend-cassandra</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>backend-cassandra</name>
<description>Cassandra Backend for </description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<!-- Generate test jar too -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Main Project Pom:
<?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>de.mycompany.mypackage</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>project</name>
<description>MyProject</description>
<modules>
<module>backend-cassandra</module>
<module>application</module>
<module>restful</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<version>1.2.3.RELEASE</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Configuration Class
#Configuration
public class CassandraConfiguration extends AbstractCassandraConfiguration {
private static final AtomicReference<CassandraConfigurationFile> config
= new AtomicReference<>(new CassandraConfigurationFile("cassandra.ini"));
#Override
protected String getKeyspaceName() {
return config.get().getKeyspace();
}
#Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster =
new CassandraClusterFactoryBean();
cluster.setContactPoints(config.get().getHostname());
cluster.setPort(config.get().getPort());
cluster.setProtocolVersion(ProtocolVersion.V4);
cluster.setTimestampGenerator(getTimestampGenerator());
cluster.setKeyspaceCreations(getKeyspaceCreations());
return cluster;
}
#Bean
public CassandraSessionFactoryBean session(){
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(getKeyspaceName());
session.setConverter(cassandraConverter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
#Bean
public CassandraConverter cassandraConverter(CassandraMappingContext mappingContext,
CassandraCustomConversions cassandraCustomConversions) {
MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext);
converter.setCustomConversions(cassandraCustomConversions);
return converter;
}
#Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
#Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
#Override
public String[] getEntityBasePackages() {
return new String[]{"myproject.cassandra.domain"};
}
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
List<CreateKeyspaceSpecification> createKeyspaceSpecifications = new ArrayList<>();
createKeyspaceSpecifications.add(getKeySpaceSpecification());
return createKeyspaceSpecifications;
}
private CreateKeyspaceSpecification getKeySpaceSpecification() {
return CreateKeyspaceSpecification.createKeyspace(config.get().getKeyspace())
.ifNotExists(true)
.withNetworkReplication(DataCenterReplication.of("dcl",3L));
}
#Bean
CassandraCustomConversions cassandraCustomConversions() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(new DateToTimestampConverter());
return new CassandraCustomConversions(converters);
}
}

No qualifying bean of type 'org.springframework.cloud.client.discovery.DiscoveryClient' available

What else is required for DiscoveryClient?
I am trying to fetch registered services to eureka using DiscoveryClient.
The standalone program works well, but the following code snippet is to be integrated with a web application which runs on JBoss.
Here is the code
DiscoveryClientController.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
#Controller
public class DiscoveryClientController {
#Autowired
private DiscoveryClient discoveryClient;
public DiscoveryClient getClient() {
return discoveryClient;
}
}
DiscoveryClientBean.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class DiscoveryClientBean {
#Bean
public DiscoveryClientController discoveryClientController() {
return new DiscoveryClientController();
}
}
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>com.test</groupId>
<artifactId>Discovery</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<!-- <sourceDirectory>src</sourceDirectory> -->
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<spring.version>4.3.8.RELEASE</spring.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
<!-- <version>1.3.4.RELEASE</version> -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.5.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</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>
</project>
The exception is telling you that you are missing the definition of the spring bean of type org.springframework.cloud.client.discovery.DiscoveryClient.
In this particular case, you don't have to define one yourself, but you have to enable it with the #EnableDiscoveryClient annotation on your #SpringBootApplication annotated class.
If you need extra details, please take a moment to read Service Registration and Discovery
you can use this controller aside your bootstrap config:
#RestController
class ServiceInstanceRestController {
#Autowired
private DiscoveryClient discoveryClient;
#RequestMapping("/service-instances/{applicationName}")
public List<ServiceInstance> serviceInstancesByApplicationName(
#PathVariable String applicationName) {
return this.discoveryClient.getInstances(applicationName);
}
}

Categories