Spring Cloud Contract ContractVerifierTest.java not generated - java

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.

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?

lombok and #Valid not working together spring boot 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;

Cucumber feature file not executing from Maven

I'm trying to execute my cucumber feature file using maven command mvn clean install but it's not picking my Test class. I'm able to run the feature file using my IDE IntelliJ but not working from command line. Please find my code and maven dependencies.
What I'm missing here, why mvn clean install not picking the RunTest and executing the step definitions from here MyStepdefs.
Any help would be really appreciated, I've been struggling from past two days - not sure what I'm doing wrong here.
<?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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>6.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>datatable</artifactId>
<version>3.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>6.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<properties>
<configurationParameters>
cucumber.plugin=pretty,html:target/cucumber.html
cucumber.publish.quiet=true
cucumber.publish.enabled=false
</configurationParameters>
</properties>
</configuration>
</plugin>
</plugins>
</build>
</project>
test.feature
Feature: To retrieve the customer with customer details
Scenario: retrieve the customer with customer id
Given the customer saved with customer name "john" and customer id 100
When the client calls GET "/customer/{customerId}" with customer id as 100
RunTest.java
import io.cucumber.junit.platform.engine.Cucumber;
#Cucumber
public class RunTest {
}
MyStepdefs.java
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
public class MyStepdefs {
#Given("the customer saved with customer name {string} and customer id {int}")
public void the_customer_saved_with_customer_name_and_customer_id(String string, Integer int1) {
System.out.println("Entering into given");
}
#When("the client calls GET {string} with customer id as {int}")
public void the_client_calls_get_with_customer_id_as(String string, Integer int1) {
System.out.println("Entering into when");
}
}
CucumberSpringConfig.java
import io.cucumber.spring.CucumberContextConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
#CucumberContextConfiguration
#SpringBootTest(classes = DemoApplication.class)
public class CucumberSpringConfig {
}
This is my folder structure:
Folder Structure
https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#use-the-cucumber-annotation
Cucumber will scan the package of a class annotated with #Cucumber for feature files.
To use this feature, add the #Cucumber annotation to the test runner. Doing so will make Cucumber run the feature files in the package containing the test runner.
So because your annotated class is in the com.example.demo package put the features in src/test/resources/com/example/demo.

spring validation for requestparam not working

I am trying to validate my parameter in controller using #Min(1) annotation. When I test it in unit test, it comes as 200. I am not sure what I am doing wrong. Here is my code:
Controller:
#GetMapping(produces = "application/json")
public Response search(#RequestParam(name = "firstname", required = false) String firstName,
#RequestParam(name = "lastname", required = false) String lastName, #RequestParam("ibid") #Min(1) long ibId,
#RequestParam(name = "workstationids", required = false) List<Long> workstationIds,
#RequestParam("timerange") int timeRange,
#RequestParam(name = "receivingstatus", required = false) String receivingStatus,
#RequestParam(name = "displaystart", required = false) Integer displayStart,
#RequestParam(name = "displaylength", required = false) Integer displayLength,
#RequestParam("timezone") #NotBlank String timeZone) {
...
...
Test:
#Test
public void searchWithInvalidParams() throws Exception {
mockMvc.perform(get(BASE_URL)
.param(COLUMN_IB_ID, INVALID_IB_ID_VALUE) //invalid ib id is "-1"
.param(COLUMN_TIME_RANGE, TIME_RANGE_VALUE)
.param(COLUMN_TIME_ZONE, TIME_ZONE)
.andExpect(status().isBadRequest());
}
I am expecting it to return 400, but result is 200.
Update:
So I added #Validated in controller and added dependency to pom and it still doesn't work(still 200)
my 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>
<parent>
<groupId>com.nuance.powershare</groupId>
<artifactId>psh-app-dispatch-reporter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>psh-app-dispatch-reporter-service</artifactId>
<properties>
<sqljdbc4.version>4.0.0</sqljdbc4.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-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.nuance.powershare</groupId>
<artifactId>psh-lib-dispatch-reporter-model</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqljdbc4.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.10.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
You have done the following 2 steps correctly:
Included the hibernate validator to the pom.xml
Added #Validated annotation to the controller
Two more steps are required. Can you do these two in addition to above:
Add one more entry to the pom.xml (not sure about why the javax.el is needed)
<dependency>
<groupId>org.glassfish </groupId>
<artifactId>javax.el </artifactId>
<version>3.0.1-b11 </version>
</dependency>
Add the following to your Java Configuration class
#Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
(refer - https://www.baeldung.com/spring-validate-requestparam-pathvariable)
Starting with Boot 2.3, the required steps for #RequestParam and #PathVariable validation are:
Add dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Add #Validated to controller class
Source: https://www.baeldung.com/spring-boot-bean-validation

Categories