Spring Bean Creation Exception - java

To understand what real problem in Spring is really needs well knowledge about structure. I am seaching about this issue for two days and cant find any solution. In my project, using Hibernate when I build it gives the error message below:
2013-01-17 06:28:50,251 INFO [org.hibernate.cfg.SettingsFactory] - Database ->
name : MySQL
version : 5.0.96-community-nt
major : 5
minor : 0
2013-01-17 06:28:50,251 INFO [org.hibernate.cfg.SettingsFactory] - Driver ->
name : MySQL-AB JDBC Driver
version : mysql-connector-java-5.1.12 ( Revision: ${bzr.revision-id} )
major : 5
minor : 1
2013-01-17 05:13:45,060 ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'langService':
Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'langDao' defined in URL [jar:file:/C:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/NCFrameworkAdmin/WEB-INF/lib/NCFramework-0.0.1-SNAPSHOT.jar!/com/ns/commerce/framework/lang/dao/LangDaoImpl.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type [org.hibernate.SessionFactory]: : Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/admin-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in db.nc_alert_log for column alerted. Found: bit, expected: TINYINT(1) DEFAULT 0; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring/admin-hibernate.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Wrong column type in db.nc_alert_log for column alerted. Found: bit, expected: TINYINT(1) DEFAULT 0
The interesting thing is blogController, langServices and LangDao is related, however they have nothing about "nc_alert_log" table.
LangDaoImpl
package com.ns.commerce.framework.lang.dao;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import com.ns.commerce.framework.lang.model.Lang;
import com.ns.commerce.framework.generic.dao.GenericDAOImpl;
#Repository("langDao")
public class LangDaoImpl extends GenericDAOImpl<Lang, Long> implements LangDao {
#Autowired
public LangDaoImpl(#Qualifier("sessionFactory") SessionFactory sessionFactory) {
this.setSessionFactory(sessionFactory);
}
#Override
public Lang findByLocaleCode(String localeCode) {
Criteria criteria = getCriteria();
criteria.add(Restrictions.eq("localeCode", localeCode));
return findByCriteriaFirst(criteria);
}
#Override
public Lang findBySubdomain(String subdomain) {
Criteria criteria = getCriteria();
criteria.add(Restrictions.eq("subdomain", subdomain));
return findByCriteriaFirst(criteria);
}
#Override
public Lang findDefaultLang() {
Criteria criteria = getCriteria();
criteria.add(Restrictions.eq("defaultFlag", true));
return findByCriteriaFirst(criteria);
}
}
AlertLog.Java Model
#Column(name="alerted", columnDefinition = "TINYINT(1) DEFAULT 0")
private int alerted;
On the Database: Alerted Column > TINYINT(1) and deafult value is 0.
DB.properties
hibernate.hbm2ddl.auto=validate
#hibernate.hbm2ddl.auto=create-drop
hibernate.hbm2ddl.import_files=/import_standard.sql
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.generate_statistics=false
hibernate.use_sql_comments=true
hibernate.query.factory_class=org.hibernate.hql.ast.ASTQueryTranslatorFactory
hibernate.cache.use_query_cache=true
hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
hibernate.cache.use_second_level_cache=true
#-------------------------------------------------------------------------------
# MySQL Settings
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/asd?autoReconnect=true
jdbc.username=asd
jdbc.password=asd
# Property that determines which Hibernate dialect / MySQL5Dialect || MySQLDialect
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Pom.xml
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>NC_Core</artifactId>
<groupId>com.ns.commerce</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>NCFrameworkAdmin</artifactId>
<packaging>war</packaging>
<name>NCFrameworkAdmin Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.nc.commerce</groupId>
<artifactId>NCFramework</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Joda Time Library -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
</dependency>
<!--Regular Expression Libraries -->
<dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
</dependency>
<dependency>
<groupId>jakarta-regexp</groupId>
<artifactId>jakarta-regexp</artifactId>
</dependency>
<!-- Commons validator -->
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
</dependency>
<!-- Tiles -->
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-jsp</artifactId>
</dependency>
<!-- AOP dependency -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
</dependency>
</dependencies>
<build>
<finalName>NCFrameworkAdmin</finalName>
If any other source needed please comment.

Wrong column type in db.nc_alert_log for column alerted.
Found: bit, expected: TINYINT(1) DEFAULT 0
Causing the issue. Check type of alerted column in database and make sure you are using the same in AlertLog.java.

nc_alert_log and LangDao are not directly related, but LangDao and the hibernate sessionFactory are. The sessionFactory cant be created because the database definition is wrong. The eror indicates the columntype in the database isn't TINYINT, but BIT (boolean). That's why the sessionFactory fails to start. Without a sesionFactory the LangDao can't be created etc etc.

private int alerted;
try changing to:
private boolean alerted;

Related

Azure Java SDK - ChainedTokenCredential not switching to next available TokenCredential in case of 403 forbidden error

Even though ChainedTokenCredential enables multiple TokenCredential implementations to be tried in order until one of the getToken methods returns an access token, but it can handle only authentication error but not authorization i.e., it will throw 403 error and will not automatically switch to other available authentication if RBAC permissions are not defined. ChainedTokenCredential is not switching from System Assigned Managed Identity to User Assigned Managed Identity if System Assigned Managed Identity don't have RBAC permissions
DefaultAzureCredential defaultAzureCredential = new DefaultAzureCredentialBuilder().build();
ManagedIdentityCredential userAssignedmanagedIdentityCredential = new ManagedIdentityCredentialBuilder().clientId("<USER ASSIGNED MANAGED IDENTITY CLIENT ID>").build();
ChainedTokenCredentialBuilder builder = new ChainedTokenCredentialBuilder();
builder.addFirst(defaultAzureCredential);
builder.addLast(userAssignedmanagedIdentityCredential);
ConnectionPolicy defaultPolicy = ConnectionPolicy.getDefaultPolicy();
defaultPolicy.setUserAgentSuffix(applicationName);
defaultPolicy.setPreferredRegions(Arrays.asList("Central US"));
AsyncDocumentClient asyncDocumentClient = new AsyncDocumentClient.Builder().withServiceEndpoint("<Cosmos DB URL>").withTokenCredential(builder.build()).withConnectionPolicy(defaultPolicy) .withConsistencyLevel(ConsistencyLevel.EVENTUAL).build();
Below are the artifact details
<properties>
<java.version>11</java.version>
<reactor-netty>1.0.9</reactor-netty>
<reactor-core>3.4.8</reactor-core>
</properties>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.18.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-blob</artifactId>
<version>12.12.0</version>
<exclusions>
<exclusion>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-cosmos</artifactId>
<version>4.17.0</version>
<exclusions>
<exclusion>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${reactor-core}</version>
<!--$NO-MVN-MAN-VER$ -->
<!-- Please don't remove/degrade the version, possible for compatibility issues -->
</dependency>
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>${reactor-netty}</version>
<!--$NO-MVN-MAN-VER$ -->
<!-- Please don't remove/degrade the version, possible for compatibility issues -->
</dependency>

Quarkus Reactive - Multiple matching properties for name "security.jaxrs.deny-unannotated-endpoints" Error

Using Quarkus I get the following error at execution time:
Caused by: java.lang.IllegalArgumentException: Multiple matching
properties for name "security.jaxrs.deny-unannotated-endpoints"
property was matched by both public boolean
io.quarkus.resteasy.reactive.common.runtime.JaxRsSecurityConfig.denyJaxRs
and public boolean
io.quarkus.resteasy.runtime.JaxRsSecurityConfig.denyJaxRs. This is
likely because you have an incompatible combination of extensions that
both define the same properties (e.g. including both reactive and
blocking database extensions)
My pom properties are:
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<quarkus-plugin.version>1.13.3.Final</quarkus-plugin.version>
<quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
<quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
<quarkus.platform.version>1.13.3.Final</quarkus.platform.version>
And dependencies:
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt-build</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I'm just trying to stream using Multi from mutiny and Server Sent Elements:
#GET
#Produces(MediaType.SERVER_SENT_EVENTS)
#RestSseElementType(MediaType.TEXT_PLAIN)
#Path("/stream/{count}/{name}")
public Multi<String> greetingsAsStream(int count, String name) {
return service.greetings(count, name);
}
You have both classic RESTEasy (quarkus-resteasy-jsonb, quarkus-resteasy-mutiny) and RESTEasy Reactive (quarkus-resteasy-reactive). You need to pick one and stick to it.
For example, if you want RESTEasy Reactive, you'd remove quarkus-resteasy-mutiny (no need for extra dependency with RESTEasy Reactive), and replace quarkus-resteasy-jsonb with quarkus-resteasy-reactive-jsonb.
I am facing a similar problem:
java.lang.RuntimeException: java.lang.IllegalArgumentException:
Multiple
matching properties for name "security.jaxrs.deny-unannotated-endpoints"
property was matched by both public boolean
io.quarkus.resteasy.reactive.common.runtime.JaxRsSecurityConfig.denyJaxRs
and
public boolean io.quarkus.resteasy.runtime.JaxRsSecurityConfig.denyJaxRs. This
is likely because you have an incompatible combination of extensions that both
define the same properties (e.g. including both reactive and blocking database
extensions)
I am using Swagger OpenAPIGenerate:
generatorName.set("jaxrs-spec")
Gradle dependencies:
dependencies {
implementation(Libs.quarkus_resteasy){
exclude(group = "io-quarkus", module = "quarkus-resteasy-runtime")
}
implementationList(LibGroups.quarkus_rest_server)
}
Libs.quarkus_resteasy
> val quarkus_resteasy = "io.quarkus:quarkus-resteasy"
LibGroups.quarkus_rest_server
>val quarkus_rest_server = listOf(
> > "io.quarkus:quarkus-vertx",
> > "io.quarkus:quarkus-resteasy-reactive-jackson"
> > //"io.quarkus:quarkus-resteasy-reactive"
> )
After excluding the clashing module, I am still getting the same error.
Any poin

Spring boot + hikari - dataSource or dataSourceClassName or jdbcUrl is required issue

I get the following error trying to start my Spring application
ERROR 5908 --- [ main] com.zaxxer.hikari.HikariConfig : HikariPool-1 - dataSource or dataSourceClassName or jdbcUrl is required.
My application.properties file looks like this:
spring.datasource.one.jdbc-url = jdbc:postgresql://10.x.x.x:y/sampledb1
spring.datasource.one.username = someuser
spring.datasource.one.password = somepasswd
spring.datasource.one.driver-class-name = org.postgresql.Driver
spring.datasource.two.jdbc-url = jdbc:postgresql://10.x.x.x:z/sampledb2
spring.datasource.two.username = someuser
spring.datasource.two.password = somepassword
spring.datasource.two.driver-class-name = org.postgresql.Driver
And I am using DataSourceBuilder class as below:
#Configuration
public class DataSourceConfig
{
#Bean(name = "one")
#Primary
#ConfigurationProperties(prefix = "spring.datasource.one")
public DataSource dataSource1()
{
return DataSourceBuilder.create().build();
}
#Bean(name = "two")
#ConfigurationProperties(prefix = "spring.datasource.two")
public DataSource dataSource2()
{
return DataSourceBuilder.create().build();
}
}
My pom looks like this.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<avro.version>1.8.2</avro.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<version.powermock>1.6.2</version.powermock>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
</dependencies>
This was working fine earlier, but now causing some issues. And the error occurs intermittently, sometime it starts without error, other times it fails with the error.
I tried solutions suggested in the link .They don't seem to work for me.
Change jdbc-url to jdbcUrl so Hikari can find suitable driver per url.
jdbcUrl
This property directs HikariCP to use "DriverManager-based" configuration. We feel that DataSource-based configuration (above) is superior for a variety of reasons (see below), but for many deployments there is little significant difference. When using this property with "old" drivers, you may also need to set the driverClassName property, but try it first without. Note that if this property is used, you may still use DataSource properties to configure your driver and is in fact recommended over driver parameters specified in the URL itself. Default: none
This error can occur when spring looks for the properties file at the wrong location. For example, when the Java option -Dspring.config.location=... is passed to your application server at startup then your properties file of your deployed application is ignored - even if there is no properties file at spring.config.location, too.

Javax UriBuilder error [duplicate]

This question already has answers here:
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri
(8 answers)
Closed 3 years ago.
I am trying to build a REST webservice using an asynchronous response.
I have looked around this error on the web, however, none of the solutions have worked for me. I am not sure on how to go about it.
This is the code for the REST service, it has AsyncResponse, and #Suspended which are taken from jar file specified in the pom.xml, which I will provide below. The problem is, on deploying the war, I get an exception:
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:651)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.50 logs
My class is as follows:
package com.crudapp;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import javax.annotation.Generated;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
//import javax.ws.rs.core.UriBuilder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.google.gson.Gson;
import com.mysql.jdbc.StringUtils;
import dao.User;
import dao.UserDAO;
import dao.UserDAOImpl;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
#Path("/crudpath")
public class EntityResource {
private final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/spring.xml");
UserDAO userdao = null;
private final int numOfThreads = 10;
private final ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads);
// userdao.getUsers("118");
//ctx.close();
#GET
#Produces("application/json")
public Response getTupleFromDBasJSON(#QueryParam("param1") String userid, #Suspended final AsyncResponse asyncresponse ){
if(StringUtils.isNullOrEmpty(userid))
throw new ServiceException("Userid passed to the REST service /crudpath is null or empty");
userdao = (userdao==null)?
ctx.getBean("userDAO", UserDAOImpl.class)
: userdao;
Gson gson = new Gson();
Future<List<User>> futures = executorService.submit(new DAOTaskHandlerThread(userid));
List <User> users = new ArrayList<User>();
if(futures.isDone())
{
try{
users = futures.get();
if(users!= null)
return Response.status(200).entity( gson.toJson(users).toString()).build();
}
catch(Exception ex)
{
throw new ServiceException(ex);
}
}
return Response.status(200).entity(new ArrayList<User>().toString()).build();
/*// crrate a new thread.. call the DAO .. returns the result from here.
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", "value");
return Response.status(200).entity( jsonObject.toString()).build();*/
}
private class DAOTaskHandlerThread implements Callable<List<User>>{
//private UserDAO userDAO;
private String userid;
private DAOTaskHandlerThread(//UserDAO userDAO,
String useridpassed){
///this.userDAO= userDAO;
userid= useridpassed;
}
#Override
public List<User> call() throws Exception {
// TODO Auto-generated method stub
return userdao.getUsers(userid);
}
}
}
My pom.xml file for maven is as follow:
<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>RESTJerseyExample</groupId>
<artifactId>RESTJerseyExample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- spring framework just added -->
<properties>
<java-version>1.7</java-version>
<org.springframework-version>4.0.3.RELEASE</org.springframework-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- spring framework just added ends here -->
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0</version>
</dependency>
<!-- used for httpclient library -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.2</version>
</dependency>
<!-- for async response -->
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m12</version>
</dependency>
</dependencies>
</project>
AbstractMethodError are thrown when an application tries to call an abstract method.
uri is an abstract method in UriBuilder, so you need an implementation of this. This method (with String parameter) is from version 2.0 of JAX-RS specification.
You're trying to use JAX-RS 2.0 with Jersey 1.*. Instead, you need to use Jersey 2.* that implements JAX-RS 2.0 and contains an implementation to uri method.
In your pom.xml you may remove these dependencies:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m12</version>
</dependency>
And use these dependencies:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
Using this, uri method is implemented in JerseyUriBuilder class from jersey-common.
EDIT:
You need to change, in your web.xml, servlet com.sun.jersey.spi.container.servlet.ServletContainer to org.glassfish.jersey.servlet.ServletContainer and init-param from com.sun.jersey.config.property.packages to jersey.config.server.provider.packages
I would like to add one answer to this post. I faced a similar problem today and found the root cause to be another dependent jar which was Internally using an older version of Jersey/JAX-RS.
My POM before fix was:
<jersey.version>2.17</jersey.version>
...
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.ci.wrapper</groupId>
<artifactId>client-wrapper</artifactId>
<version>${clients-wrapper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.api.commons</groupId>
<artifactId>transferobjects</artifactId>
<version>3.0.2</version>
</dependency>
The problem was with "com.ci.wrapper" and "com.api.commons".
They inturn included 2 different JAR's of BraveJersey and org.apache.cxf.cxf-rt-frontend-jaxrs (2.5.1) which were using Jersey and JAX-RS 1.X versions.
After excluding the nested jar's and adding the newer version's of BraveJersey2/org.apache.cxf.cxf-rt-frontend-jaxrs(3.1.5) it got resolved.
<dependency>
<groupId>com.api.commons</groupId>
<artifactId>transferobjects</artifactId>
<version>3.0.2</version>
<exclusions>
<exclusion>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<groupId>org.apache.cxf</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.1.5</version>
</dependency>
<dependency>
<groupId>com.ci.wrapper</groupId>
<artifactId>client-wrapper</artifactId>
<version>${clients-wrapper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<artifactId>brave-jersey</artifactId>
<groupId>com.github.kristofa</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.kristofa</groupId>
<artifactId>brave-jersey2</artifactId>
<version>2.4.2</version>
</dependency>
In case you're facing a similar issue, please check if the project's or jar's included could be using an incompatible version of Jersey/Jax-RS.
In my case it's combination of both cxf-rt-frontend-jaxrs and httpclint jar needed to be removed to resolve the issue. Both of these jars have the class javax.ws.rs.core.UriBuilder, the multiple version of this class caused the issue.
My pom had a transitive dependency on both these jars, after removing it worked.
enter code here
<exclusion>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
I have faced this problem when I was trying to use a library (custom built) in one of my project.
So the problem was happening due to mismatch in the com.sun.jersey dependencies. My project was using jersey version 2.15 whereas the custom library was giving me com.sun.jersey transitively of version 1.17.
So how to find such problems.
Use gradle dependencies task to find out the dependencies (it gives nested level results which means all the transitive dependencies will also be shown)
Once you identify the problem causing dependencies. Exclude them while adding the required dependencies in the project.
For e.g.
httpRestClient is name of my custom library which I wanted to use in my project.
So here is how I have added the dependency and at the same time excluded the conflicting dependencies of group 'com.sun.jersey'
compile(httpRestClient) {
exclude group: 'com.sun.jersey'
}
This way you can use whatever library and exclude the conflicting libraries.
Thanks.
In our case culprit was this dependency
<dependency>
<groupId>org.apache.wink</groupId>
<artifactId>wink-common</artifactId>
<version>1.0-incubating</version>
</dependency>

Spring 3 with Drools 6 maven inconcistency

I have a problem integrating Drools with Spring. I am trying to make a simple Hello World spring example, I reached a moment where my simple project refuses to compile.
To work with Drools annotations like #KSession I need the drools-spring package, but it seems incompatible with Spring 3. As builder I use Maven. Here is how my .pom's looks like:
<dependencies>
<!-- Drools -->
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-decisiontables</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-api</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-internal</artifactId>
<version>6.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-spring</artifactId>
<version>6.0.0.Beta2</version>
</dependency>
<!-- Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
</dependencies>
I use very simple case of controller class:
#Controller
#RequestMapping(value = "/")
public class BaseController extends AbstractController {
private static final Logger log = Logger.getLogger(BaseController.class);
#Autowired
#KSession("ksession-rules")
KieSession mySession;
#RequestMapping(value = "/test")
public #ResponseBody
String test() {
Fact myFact = new Fact();
myFact.setFactNumber(20); //According to my rule when myFact.number>20
//then result will be set to "Hello World"
mySession.insert(myFact);
mySession.fireAllRules();
return myFact.getResult();
}
}
At this point I receive an error:
"The hierarchy of the type BaseController is inconsistent".
I noticed that if I remove drools-spring from the dependency list the project compiles successfully. However without that package I cannot deploy my project, because the deployer demands the drools' class responsible for interpreting #KSession and that class is located in drools-spring package.
After some investigation it appears that my Drools session configuration seems to have
nothing to do with the error above, so for the sake of succinctness I will not quote them. Instead of that I will mark that even if I don't make any modifications to the spring configuration and remove the KieSession from my example, making it a simple spring hello world example, I receive one and the same error out of my IDE (Eclipse): "The hierarchy of the type BaseController is inconsistent" and if I remove the drools-spring dependency the problem disappears.
It seems to me as dependency conflict.
Does anyone experienced similar problems with drools+spring?
Can someone suggest a solution to the problem?
Am I doing something wrong?
Thanks for the help!
After some research I found two things.
Drools' drools-spring package declares its own spring dependencies that are overriding my spring dependencies, which leads to spring malunctions. What I did is to manually remove the spring dependencies from drools-spring .pom file.
Another mistake that I had in the upper example is that I didn't have kie-spring as dependency. Kie-spring is mandatory for one drools bean KModuleBeanFactoryPostProcessor, which is responsible to read my kmodule-kbase-ksession configuration from the spring configuration.
After resolving these two problems my project compiles now and I can see in the code that KModuleBeanFactoryPostProcessor defines that my configuration is being read and analized as I expected it to be.

Categories

Resources