I am building a Spring MVC application and deploying to Tomcat. When I try to use the #Valid syntax, I get a compilation error:
Valid cannot be resolved to a type.
I am using Eclipse Java EE IDE for Web Developers (Helios SR2).
I have hibernate-validator.jar in my classpath, as the Spring documentation indicates, but I don't see javax.validation.* anywhere in my references.
Any help would be appreciated.
add
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
to your context and you have to have javax.validation.* classes on classpath
search for javax.validation in maven repositories or use following if you use Maven:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>com.springsource.javax.validation</artifactId>
<version>1.0.0.GA</version>
</dependency>
Related
I went through a project structure in STS where I could see a MVC pattern where controller, service,Dao , entity classes were there. But looking at pom.xml got to know that it is a web application project. But how to confirm that it is not a spring boot application..?
I m new to spring boot. All I found is there were no starter dependencies in pom.xml or any dependency has word "boot" & there was not a main method where a spring boot project starts.
So my question is which particular dependency/ parameters differentiates spring boot application from a non spring boot application ?
In general in the first part of the pom.xml you can see this tag:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
Check the pom.xml. see if it includes the spring boot dependency or any other Spring boot dependency flavors like Spring-boot-starter, spring-boot-autoconfigure,spring-boot-web etc....
Basically anything with group ID
"org.springframework.boot"
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
Additionally also look for the #SpringBootApplication annotation on top of the main class.
If you're running a Gradle project, you might see something like this inside of one of your build.gradle (or [projectName].gradle) files.
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
Or maybe
apply plugin: 'org.springframework.boot'
The above advice to search globally for org.springframework.boot is sound, just wanted to stress there might not be a pom.xml (or any .xml) files present.
I'm using Arquillian and TomEE embedded adapter to test a WAR.
In my pom.xml I have the following entry
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>arquillian-tomee-embedded</artifactId>
<version>1.7.2</version>
<scope>test</scope>
</dependency>
and in my arquillian.xml I have the following
<container qualifier="tomee" default="true">
<configuration>
[...]
<property name="javaVmArguments">-Xms2048m -Xmx2048m</property>
<property name="singleDumpByArchiveName">true</property>
<!--<property name="singleDeploymentByArchiveName">true</property>-->
[...]
</configuration>
</container>
Adding the property singleDumpByArchiveName I expected that, dumping only once the web archive for all the tests, there would be an improvement in terms of execution time, but that did not happen.
I also tried to add the Arquillian Suite Extension with the same goal
<dependency>
<groupId>org.eu.ingwar.tools</groupId>
<artifactId>arquillian-suite-extension</artifactId>
<version>1.1.2</version>
<scope>test</scope>
</dependency>
but I get the following error
java.lang.NoSuchMethodError: org.jboss.arquillian.test.spi.TestResult.setEnd(J)V
Do you have any tip on how to make these solutions work or can you suggest others to speed up the tests execution time?
first of all:
<property name="javaVmArguments">-Xms2048m -Xmx2048m</property>
is not a tomee configuration since tomee embedded doesn't fork a JVM, probably something copied/pasted from the net.
Then your error probably just means you have a dependency conflict (tomee brings an older version of arquillian). You need to fix that. You can need to use tomee 1.7.4 (or 7.x) which is compatible with arquillian > 1.1.11
A Spring web application is just a normal web application using servlet API 3.0.
In servlet API 3.0 web.xml file is optional (most of the time).
I' ve tried not to include web.xml inside my Spring aplication,but somehow it seems to be required even using Servlet 3.0
The only difference between a Spring applications and web apps I've run successfully without writing a web.xml file, is that in Spring filters and servlets are defined inside jars in the lib folder.
Any clues why this happens ?
You can't just exclude the web.xml, as you (at least) need to define which version of the Servlet API are you using.
At the minimum, you'd need this in your web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
Everything else (servlets, filters, etc.) can be configured with annotations.
Fortunately, you can create completely XML free Spring applications nowadays.
There are various different options for such configs with or without Spring Boot.
With pure Spring you can use AbstractAnnotationConfigDispatcherServletInitializer
With Spring Boot, you can have SpringBootServletInitializer.
This is my Github repository with bunch of projects based on both approaches.
Version of servlets is defined by Servlet API you have on your classpath. E.g.:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.servlet</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
or
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
You also don't specify servlets, as Spring has own implementation of servlet DispatcherServlet.
Filters are registered as beans into Spring context.
I recently switched to Apache log4j2, and still can not find a way to configure hibernate logging using log4j2.xml.
Because I can not find a way around this problem I still use log4j.properties file explicitly for hibernate. This is not the best solution since my log4j2.xml uses JPA appender (writes logs to db). I do not want to write separate logic for hibernate.
Is there a way to configure hibernate logging using log4j2?
As suggested in
https://issues.apache.org/jira/browse/LOG4J2-172
you can add system property to force hibernate use slf4j
-Dorg.jboss.logging.provider=slf4j
also log4j-slf4j-impl should be added to classpath
My custom solution:
with Spring you can place
org.jboss.logging.provider=slf4j
in property file
(envConfigLocation is file url)
<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" ref="envConfigLocation" />
<property name="order" value="1"/>
</bean>
I found an answer to this question at: How to redirect all logs from hibernate and spring to log4j2?
Basically log4j2 doesn't work with Hibernate so you have to use log4j. But you still use your log4j2 configuration. You need the following dependencies and then the magic happens in the background.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<!--HIBERNATE LOGGER (log4j)-->
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.6</version>
</dependency>
It is possible to redirect calls to the log4j-1.x API to the log4j-2.0 implementation. The FAQ about which jars to include explains how to do this. You probably need to remove the old log4j-1.x jar from the classpath when you do this.
I installed Spring Tool Suite and am now using it for a little sample project. However I keep having an error in my dispatcher-servlet.xml file:
Build path is incomplete. Cannot find class file for org/springframework/beans/factory/Aware
This error is highlighted here:
<bean
**class="org.springframework.web.servlet.view.InternalResourceViewResolver">**
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
Any thought?
I think you may have some old jar files in your configuration. Try using the most current spring libraries.
I found my self in exactly the same case when I used incompatible maven spring-dependencies i.e check Spring Security Site on the right side where it explains what spring version should be used with what spring security version.
The project is missing the servlet-api jar and the error can be resolved by adding the dependency in the maven pom file
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
you can put this dependency with your spring version :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
<scope>compile</scope>
</dependency>
If you are using eclipse:
Right click on root project -> properties.
Click on Deployment Assembly.
Click add button.
Double click on Java Build Path Entries and Select build path
entries to include. (maybe if you are using maven, you need also
include those dependencies).
Finally clean and build.
This worked for me.