Throwing Validation Exception in Spring boot - spring-mvc

I am creating a Spring boot project for testing my rest API (Very Very tiny Project). The Project contains three files.
1) App.java (Main Program)
2) AppConfig (Configuration file)
3) Main Controller (Simple Rest controller)
I added Spring-boot-starter-parent (1.4.0.RELEASE) and spring-boot-starter-web as dependency.
But While executing,getting an exception like follows.
"javax.validation.ValidationException: Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath."
I added the following dependency and the error gone.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.3.4.Final</version>
</dependency>
My question is, what is the root cause of the specific error message?
Why we are adding the validator?

It means the classloader could only find the API of bean validator. You have to add some implementation package into the classpath.
Like:
org.glassfish.jersey.ext:jersey-bean-validation

Related

I want to use CDI with Java SE 12, but start up failes (Main Class, JBoss or Payara, all the same error)

I got completely stuck. I want to use CDI2 with Servlets to write a simple web app. However, the beans not get loaded.
I get the following error in JBoss or Payara or Weld (if running as Main Application):
WELD-001524: Unable to load proxy class for bean Managed Bean [class MyBean] with qualifiers [#Any #Default]
Does anyone still use CDI2 or has a running modern example?
I compile with maven.
warm regards,
Alex
I found it out myself. I had the wrong dependencies. I got confused because of Jakarta. I used the CDI-2 maven dependency. So I updated to Jakarta. Right? This is the way to go, right?
I can deploy. Payara-Micro works. JBoss (WildFly should do the same)
However, Payara Micro does not get track of the URL patterns of simple Servlets, although still, one can call it over the client. (Intellij has a CDI tab, gives a nice overview itself)
...
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>8.0.0</version>
<scope>provided</scope>
</dependency>

AssertJ Swagger test is stuck on executing

I'm trying to adopt Swagger in REST API development (Spring Boot web application). API documenting process and code generation based on swagger spec works good, and now I've faced a problem writing integration tests using assertj-swagger and SpringFox libraries.
A few little words about these libraries. Springfox works by examining an application, once, at runtime to infer API semantics based on Spring configurations, class structure and various compile time java annotations. The swagger-assertj test library should compare a contract-first Swagger YAML file with a code-first Swagger JSON generated by SpringFox. For Consumer Driven Contract tests, assertj-swagger fails a test if it finds missing resources, methods, models, or properties in the implementation which are required by the consumer specification.
My test looks like (test code is taken from GitHub example):
#RunWith(SpringRunner.class)
#SpringBootTest
public class AssertJSwaggerConsumerDrivenTest {
#Test
public void validateThatImplementationSatisfiesConsumerSpecification() {
String designFirstSwagger = AssertJSwaggerConsumerDrivenTest.class.getResource("/swagger.yaml").getPath();
SwaggerAssertions.assertThat("http://localhost:8080/v2/api-docs")
.satisfiesContract(designFirstSwagger);
}
}
The problem is that this test is executed for a long time and seems to get stuck cause I don't see any log output after this line:
INFO c.s.e.AssertJSwaggerConsumerDrivenTest : Started AssertJSwaggerConsumerDrivenTest in 24.03 seconds (JVM running for 26.774)
I'm sure that SpringFox is working, cause I see JSON after opening GET http://localhost:8080/v2/api-docs in my browser.
I have no compile-time or build-time errors when running test, cause Maven resolved dependencies and Spring Boot context is initialized successfully.
Is there anybody experienced in using assertj-swagger cause it looks like I'm doing something wrong?
I've managed to run my AssertJSwaggerConsumerDrivenTest! I think I've never been so happy to see a lot of red test results before =) Anyway it's better than running test forever.
AssertJ-Swagger's readme is a little bit outdated. Here's what I changed to fix the problem.
Test code:
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AssertJSwaggerConsumerDrivenTest {
#LocalServerPort
int randomPort;
#Test
public void validateThatImplementationSatisfiesConsumerSpecification() {
File designFirstSwagger = new File(AssertJSwaggerConsumerDrivenTest.class.getResource("/swagger.yaml").getFile());
SwaggerAssertions.assertThat("http://localhost:" + randomPort + "/v2/api-docs")
.satisfiesContract(designFirstSwagger.getAbsolutePath());
}
}
pom.xml:
<dependency>
<groupId>io.github.robwin</groupId>
<artifactId>assertj-swagger</artifactId>
<version>0.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-parser</artifactId>
<version>1.0.30</version>
<scope>test</scope>
</dependency>
Also I got java.lang.OutOfMemoryError: Permgen space so I had to add this -XX:MaxPermSize=384m to VM options.

ClassMode cannot be resolved to a variable

In my java spring mvc app, I am trying to reset a bunch of records for test with Junit.
but in line:
#DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
It complains with:
ClassMode cannot be resolved to a variable
Update:
I have added
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
</dependency>
but then it complains with other lines
#ActiveProfiles("test")
#Sql(scripts="requests-dataset.sql")
#DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
the error is:
ActiveProfiles cannot be resolved to a type
Sql cannot be resolved to a type
Sql cannot be resolved to a type
First of all, the class name is DirtiesContext.ClassMode (it's a nested class of DirtiesContext).
Second, as any other class, it must be imported.
Third, as the javadoc shows, it exists since Spring 3.0. So you won't find it in Spring 2.5. Use the same version of spring-test as the version used for the rest of the spring framework libraries that you're using.

When Spring Boot application running by Intelij Idea views can`t be resolved

I have encountered a strange situation, the decision which I cant find.
I`m runnig simple demo application using Spring Boot 1.3.0 and Intelij Idea 14.1.3
The problem is that Spring MVC cant resolve the view:
javax.servlet.ServletException: Could not resolve view with name 'home' in servlet with name 'dispatcherServlet'
Oddity is that when I run application by Maven Spring Boot-plugin
mvn clean spring-boot:run
everythig works fine.
Both views ("home.jsp" - returning from Controller and "start.jsp" - described in Configuration class) resolve correctly.
Full source code you can see here
I`ve downloaded another demo project - the same situation.
So, I think that something wrong with my IDE configuration.
But what is going wrong - I don`t know.
In File->Project Structure
I have added Spring and web module in "Modules" tab, the same I`ve made in "Facets" tab.
What is possible to do to make application run correctly using IDE?
Problem was in dependecies configuration in pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
I have change <scope>provided</scope> to <scope>compile</scope> and it works well.

Standalone maven package for hibernate validator annotations

A quick question: is there a stand-alone package for org.hibernate.validator.constraints.*?
I want to put these annotations on a DTO package, but don't want to force the user to have hibernate-validator on their class path.
I don't really know what you mean by standalone in this context but here is a link to the 4.3.1 version of hibernate-validator.
Adding this to your pom will give you access to all the annotations in org.hibernate.validator.constraints package:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.0.Final</version>
</dependency>
mvnrepository.com is a very good place to search for maven artifacts.
Edit
Ok now I understand your question, you only want the annotations in the org.hibernate.validator.constraints as a dependency w/o all the other validation stuff.
Then my answer is no, there is no such thin jar. You will have to use the hibernate-validator to get the annotations.
If you use custom Hibernate Validator constraints you would have to add the actual core classes as well anyways. As long as you rely on Bean Validation constraints (javax.validation.*) it is enough to add the Bean Validation API jar.
What is your concen with adding the hibernate Validator jar?

Resources