I'm using Wildfly 10, Jersey, and injecting dependencies with #Inject. I have DAO and Service interfaces with their implementations declared in a CustomBinder. The injections work well, but the EntityManager is injected null with the #PersistenceContext annotation. I'm using MySQL and the datasource test-connection is working.
API Rest class
#Path("/account")
public class CuentaServiceRS {
#Inject
private ICuentaService cuentaService;
#GET
#Produces(MediaType.APPLICATION_JSON)
public Cuenta getCuenta() {
return cuentaService.getCuentas().get(0);
}
}
The implementation of ICuentaService
#Stateless
public class CuentaServiceImpl implements ICuentaService {
#Inject
private ICuentaDAO cuentaDAO;
#Override
public List<Cuenta> getCuentas() {
List<Cuenta> cuentas = cuentaDAO.getAllCuentas();
cuentas;
}
}
The implementation of CuentaDAO
#Stateless
public class CuentaDAOImpl implements ICuentaDAO {
#PersistenceContext(unitName = "altitudePU")
protected EntityManager em;
#Override
public List<Cuenta> getAllCuentas() {
CriteriaQuery<Cuenta> cq = em.getCriteriaBuilder().createQuery(Cuenta.class);
/...
return resultlist;
}
}
My persistence-unit in persistence.xml
<persistence-unit name="altitudePU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/AltitudePU</jta-data-source>
<class>ar.com.olx.domain.Cuenta</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
The servlet configured on web.xml
<servlet>
<servlet-name>altitudeservlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>ar.com.villat.bind.ApplicationJaxRS</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
My custom AplicationJaxRS which extendes from ResourceConfig
public class ApplicationJaxRS extends ResourceConfig {
public ApplicationJaxRS(){
register(new CustomBinder());
packages(true, "ar.com.olx");
}
}
The CustomBinder
public class CustomBinder extends AbstractBinder {
#Override
protected void configure() {
bind(CuentaServiceImpl.class).to(ICuentaService.class);
bind(CuentaDAOImpl.class).to(ICuentaDAO.class);
}
}
And finally, my pom.xml dependencies related to this post
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
<version>1.0.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
<scope>provided</scope>
</dependency>
If you need more information, please tell me.
As you are using a full stack JavaEE server you can simplify this a lot.
If you have not added a MySQL datasource to WildFly then you can do it like this:
WildFly Datasource Setup
Create a file called deploy-mysql-ds.cli with the following content:
# Execute offline
embed-server --server-config=standalone.xml
deploy $HOME/.m2/repository/mysql/mysql-connector-java-5.1.39.jar
# Add the application datasource
data-source add \
--name=AltitudeDS \
--driver-name=mysql-connector-java-5.1.39.jar \
--connection-url=jdbc:mysql://localhost:3306/altitudeDB \
--jndi-name=java:jboss/datasources/AltitudePU \
--user-name=$USER_NAME \
--password=$PASSWORD
Replace $HOME, $USER_NAME and $PASSWORD with real values
Configure the data source in Wildfly by executing it like:
$JBOSS_HOME/bin/jboss-cli.sh --file="deploy-mysql-ds.cli"
You only need to do this once.
Note that the --jndi-name matches the <jta-data-source>...</jta-data-source> in
your persistence.xml file.
You do not need the mysql-connector-java-5.1.39.jar in your maven dependencies because it has been added to the server directly. There are other solutions that involve adding the jar as a "module"
but I find this way to be much simpler.
Amend Application
Amend your application as follows:
Remove all the redundant dependencies:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
Remove the AplicationJaxRS and CustomBinder classes and add a JAXRSConfiguration class:
/**
* Configures a JAX-RS endpoint
*/
#ApplicationPath("resources")
public class JAXRSConfiguration extends Application {
}
Remove the altitudeservlet from the web.xml. If that is the only thing in it then completely remove the whole file.
Test it
You should be able to deploy this web application and access it from:
http://localhost:8080/altitude-webapp/resources/account
where altitude-webapp is the name of your WAR file.
Related
everyone!
I’m trying for the first time in my life to use a pool of connections. I want to apply it to my project on Java + Tomcat + maven + Spring MVC + Spring Security + Hibernate. As a library with the implementation of a connections pooling, I decided to use c3p0 library.
I added the dependencies for c3p0 to 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.example</groupId>
<artifactId>first-rest-project</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>FirstRestProject</name>
<description>CRUD methods testing in the REST API app</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<failOnMissingWebXml>false</failOnMissingWebXml>
<springframework.version>5.1.7.RELEASE</springframework.version>
<springsecurity.version>5.1.5.RELEASE</springsecurity.version>
<hibernate.version>5.2.8.Final</hibernate.version>
<jackson.library>2.9.8</jackson.library>
</properties>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring ORM -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Hibernate ORM -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- Hibernate-c3p0 Integration -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- Jackson API for JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.library}</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${springsecurity.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!-- JSP API -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Embedded Apache Tomcat required for testing war -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
I added c3p0’s connection pooling settings to file db.properties:
# MySQL properties
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/service
mysql.user=root
mysql.password=****
# Hibernate properties
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
#C3P0 properties
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=2
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.idle_test_period=3000
hibernate.c3p0.max_statements=0
I think this is enough to c3p0’s connection pool work. But I do not know whether it works or not.
When I run my application (in IDE), I see that there are minPoolSize->1 and maxPoolSize->2 in the server logs. But how I can test it in practice? If I used JDBC requests, I would try to create multiple Connections. But I work with ORM, in other words with sessions. How to do it there? I tried the following: I sent 3 GET requests from 3 different browsers (they execute access to the database), but despite the fact that maxPoolSize=2, all 3 ones were executed successfully.
The code of my DAO class:
package com.example.dao;
import com.example.model.User;
import org.hibernate.*;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.persistence.criteria.*;
import java.util.List;
#Repository
public class UserDaoImpl implements UserDao{
#Autowired
private SessionFactory sessionFactory;
#Override
public Integer save(User user) {
sessionFactory.getCurrentSession().save(user);
return user.getId();
}
#Override
public User get(Integer id) {
return sessionFactory.getCurrentSession().get(User.class, id);
}
#Override
public User get(String username) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from User u where u.username = :login");
query.setParameter("login", username);
return (User) query.getResultList().get(0);
}
#Override
public void update(Integer id, User user2) {
Session session = sessionFactory.getCurrentSession();
User user1 = session.byId(User.class).load(id);
user1.setUsername(user2.getUsername());
user1.setPassword(user2.getPassword());
user1.setId_role(user2.getId_role());
user1.setName(user2.getName());
user1.setSurname(user2.getSurname());
user1.setEmail(user2.getEmail());
user1.setBirth(user2.getBirth());
session.flush();
}
#Override
public void delete(Integer id) {
Session session = sessionFactory.getCurrentSession();
User user = session.byId(User.class).load(id);
session.delete(user);
}
#Override
public List<User> list() {
Session session = sessionFactory.getCurrentSession();
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery<User> cq = cb.createQuery(User.class);
Root<User> root = cq.from(User.class);
cq.select(root);
Query<User> query = session.createQuery(cq);
return query.getResultList();
}
}
The code of my Hibernate configuration class:
package com.example.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import static org.hibernate.cfg.AvailableSettings.*;
import java.util.Properties;
/*
Hibernate configuration class
*/
#Configuration
#PropertySource("classpath:db.properties")
#EnableTransactionManagement
#ComponentScans(value = { #ComponentScan("com.example.dao"), #ComponentScan("com.example.service")})
public class HibernateConfig {
#Autowired
private Environment env;
#Bean
public LocalSessionFactoryBean getSessionFactory(){
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
props.put(DRIVER, env.getProperty("mysql.driver"));
props.put(URL, env.getProperty("mysql.url"));
props.put(USER, env.getProperty("mysql.user"));
props.put(PASS, env.getProperty("mysql.password"));
// Setting Hibernate properties
props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
// Setting C3P0 properties
props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
props.put(C3P0_ACQUIRE_INCREMENT, env.getProperty("hibernate.c3p0.acquire_increment"));
props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
props.put(C3P0_IDLE_TEST_PERIOD, env.getProperty("hibernate.c3p0.idle_test_period"));
props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("com.example.model");
return factoryBean;
}
#Bean
public HibernateTransactionManager getTransactionManager(){
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
}
Does connection pooling work in my case? How I can check it out? Any ideas?
Thanks in advance for any answers or links.
In MySQL you can use show processlist to check the active connections and their various properties at any one time.
I have class that one of it's parameters i want to set from properties file:
import org.springframework.beans.factory.annotation.Value;
(..)
#Getter
#Setter
#NoArgsConstructor
public class ConvertNonStandardOfferRequestDtoWrapper {
private ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto;
#Value("true")
private boolean documentPrintoutsRequired;
public ConvertNonStandardOfferRequestDtoWrapper(ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto) {
this.convertNonStandardOfferRequestDto = convertNonStandardOfferRequestDto;
}
}
What i see inside constructor is that documentPrintoutsRequired is false instead of true. I see that when debuging and setting breakpoint inside constructor. And i have a pom file for this module:
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>x</groupId>
<artifactId>policy</artifactId>
<version>4.0-SNAPSHOT</version>
</parent>
<artifactId>policy-api</artifactId>
<dependencies>
<dependency>
<groupId>x</groupId>
<artifactId>common-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
<build>
(...)
</build>
</project>
I am wonderning why #value does not work correctly ?
I'd advise you use constructor inyection for all attributes, this way you'll see the injected #Value during construction time.
Besides the class must be a Spring bean, so add #Component annotation:
#Component
#Getter
#Setter
#NoArgsConstructor
public class ConvertNonStandardOfferRequestDtoWrapper {
private ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto;
private boolean documentPrintoutsRequired;
public ConvertNonStandardOfferRequestDtoWrapper(ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto,
#Value("${yourproperty}") boolean documentPrintoutsRequired) {
this.convertNonStandardOfferRequestDto = convertNonStandardOfferRequestDto;
this.documentPrintoutsRequired = documentPrintoutsRequired;
}
}
You can read the value from properties file, such as
username = Tom.
use #Value in Java, you can set a default value like this:
#Value("${username:Jack}")
If the username does not exist in properties file, it will be "Jack".
Did you try this:
#Value("${yourPropInPropertiesFile}")
private boolean documentPrintoutsRequired;
I'm trying to understand how to use #QuerydslPredicate but my test API fails when it is called:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No primary or default constructor found for interface com.querydsl.core.types.Predicate] with root cause
java.lang.NoSuchMethodException: com.querydsl.core.types.Predicate.< init >()
This is the method in my Controller class:
#ResponseBody
#RequestMapping(value = "/user/query", method = RequestMethod.GET)
public Iterable<User> getByCriteria(#QuerydslPredicate(root = User.class) Predicate predicate)
{
return this.userDao.getByCriteria(predicate);
}
I've used this spring blog post and this example to try and implement my API but I don't understand what I am doing wrong.
Edit #1
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.example</groupId>
<artifactId>email-encrypt</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<name>email-encrypt</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<querydsl.version>4.1.4</querydsl.version>
<springfox.version>2.6.1</springfox.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-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.59</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcmail-jdk15on</artifactId>
<version>1.59</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.14.1.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${springfox.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>${build.directory}/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Edit #2
I've create a sample project here.
If you comment out the #Configuration on the swagger configuration class it will work. I'm still trying to understand why, I guess that with that annotation the way spring loads the configuration is different and this is causing the issue.
//#Configuration
#EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurationSupport
I think your problem lies in your pom.xml. Be sure you are using compatible version of query-dsl. For instance if you use spring-data-jpa 2.0.8, you should use querydsl-* 4.1.4+
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-*</artifactId>
<version>4.1.4</version>
</dependency>
you can check in maven repository which version you need
Edit 1 try to add the querydsl-core to your maven:
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-core</artifactId>
<version>4.1.4</version>
</dependency>
Extending on #cristobalrosa's answer, this might be due to web application not being configured by Spring Boot. For instance my project also had a SwaggerConfig extending WebMvcConfigurationSupport:
#Configuration
#EnableSwagger2
public class SwaggerConfig extends WebMvcConfigurationSupport {
// Docket bean
#Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
}
}
I removed the inheritance and manual resource handlers, and it works fine now.
Note: In addition to WebMvcConfigurationSupport, things like #EnableWebMvc & WebMvcConfigurer might also lead to Spring Boot's web autoconfiguration not being used.
Sources: Swagger Issue comment
Same error with me.
I just want to share the situation to may give some hints. So look at my config and dependencies and read articles that I linked. try #EnableWebMvc instead of 'WebMvcConfigurationSupport'
Error
java.lang.IllegalStateException: No primary or default constructor found for interface com.querydsl.core.types.Predicate
Caused by: java.lang.NoSuchMethodException: com.querydsl.core.types.Predicate.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3427)
at java.base/java.lang.Class.getDeclaredConstructor(Class.java:2631)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:216)
dependencies
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation "org.springframework.data:spring-data-commons"
implementation "com.querydsl:querydsl-apt:4.3.1"
implementation "com.querydsl:querydsl-jpa:4.3.1"
}
Querydsl web support is available in spring-data-commons since 1.11
from https://www.baeldung.com/rest-api-search-querydsl-web-in-spring-data-jpa
Web Mvc Config
In my case, I have to implements WebMvcConfigurer and add #EnableWebMvc instead of WebMvcConfigurationSupport. I don't know why #EnableWebMvc is needed even I had extended WebMvcConfigurationSupport with #Configuration. I just guess WebMvcConfigurationSupport doesn't implements init() of Predicate.
#Configuration
//#EnableSpringDataWebSupport // <-- doesn't necessary for me
#EnableSwagger2
#EnableWebMvc // <-- add
public class SwaggerConfig implements WebMvcConfigurer { //<-- instead of 'WebMvcConfigurationSupport'
...
}
QueryDSL web support From Spring document
The feature will be automatically enabled along #EnableSpringDataWebSupport when Querydsl is found on the classpath.
https://docs.spring.io/spring-data/jpa/docs/1.9.0.RELEASE/reference/html/#core.web.type-safe
Controller
#GetMapping
public List<InputMethodDto.Response> getInputMethodTypeList(
#QuerydslPredicate(root = InputMethod.class) Predicate predicate) {
return service.getInputMethodList(predicate);
}
Repository
public interface InputMethodRepository extends JpaRepository<yourEntity, Long>, QuerydslPredicateExecutor<yourEntity>, QuerydslBinderCustomizer<QyourEntity> {
#Override
default void customize(final QuerydslBindings bindings, final QyourEntity root) {
bindings.bind(String.class).first((StringPath path, String value)-> path.eq(value));
}
}
I have created a small Jersey application which has the following resource class.
#Path("/tests")
public class TestController {
#GET
#Produces("application/json")
public Response testJob(#PathParam("testName") String testName) {
return Response.ok("test").build();
}
}
Below is my application class.
#ApplicationPath("/")
public class ApplicationConfig extends Application {
// All request scoped resources and providers
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(TestController.class);
return classes;
}
// all singleton resources and providers
#Override
public Set<Object> getSingletons() {
Set<Object> singletons = new HashSet<>();
return singletons;
}
}
Below is my running configuration in intellij.
I just downloaded the glass fish and did no external configurations. I searched online for the default user name password for domain and gave it them as admin and admin respectively. But when I run the application using the URL,
http://localhost:8080/test-processor/tests
I am getting a 404 error saying the requested resource is not found. My war module name is test-processor. I have tried the url http://localhost:8080/tests with no luck as well. What am I doing wrong here? Any help would be much appreciated. Below is the list of dependencies I have added in my pom xml.
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.18.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.19.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
I am succesfully running EJB tests with Arquillian against an (older) Glassfish 3.1 embedded.
When trying to #Inject weld beans, I get a NPE when I want to address a method in a transitively injected Weld bean.
So, I am testing EJB A. EJB A has a ref to EJB B. EJB B has a ref to a Weld bean.
This is the Jar init.
#Deployment
public static Archive<?> createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "itscool.jar")
.addClasses(DashboardService.class, PropertyService.class, StorageService.class, EPSRepository.class,
EmailService.class, NotificationRepository.class, EmailRepository.class, Configuration.class,
MailingList.class, BogusBean.class, Bogus.class)
.addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "META-INF/beans.xml")
.addAsResource(new File("src/test/resources/csman.properties"), "csman.properties");
}
So, the injected bean is the Configuration.class. First, I thought there was some initialization error in the Configuration impl class, so I created the #Bogus and BogusBean to only test the injection. I get the same errors.
#Bogus
public class BogusBean {
public void setIt(){
}
}
And the Qualifier
#Documented
#Target({ ElementType.METHOD, ElementType.FIELD, ElementType.TYPE })
#Retention(RetentionPolicy.RUNTIME)
#Qualifier
public #interface Bogus {
}
And the injection in the EmailService (a #stateless session bean).
#Inject
#Bogus
private BogusBean bogus;
#EJB
private EmailRepository emailRepos;
#PostConstruct
public void init() {
bogus.setIt();
}
The NPE happens on the bogus.setIt() line. as bogus == null.
This is the maven config
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.1.11.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.core</groupId>
<artifactId>arquillian-core-api</artifactId>
<version>1.1.11.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.1.2.Final</version>
</dependency>
I added a Profile for the glassfish embedded
<profile>
<id>arquillian-glassfish-embedded</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2.2</version>
<!-- was 4.0 -->
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
I also tried to change from weld-ee to weld-se but then the container doesn't even start. (as stated here) : how-to-test-ejb-method-call-with-arquillian-weld-ee-embedded