IDEA Build got error: package org.springframework.web.servlet.config.annotation does not exist

When building project in IDEA 2021.1, following error appeared:

package org.springframework.web.servlet.config.annotation does not exist

And the weired thing is that running mvn compile in command line works well, but run Build in IDEA will got this error.

The building process works before, but recently I modified source code of one of my dependency JHCommonLibrary which is depending on spring-webmvc

I remove spring-webmvc dependency from JHCommonLibrary but it didn't work.

Then add spring-webmvc explicitly, and it didn't work as well.

Solution

Finally, I move JHCommonLibrary to end of <dependencies>, now it works. Or adding spring-webmvc dependency before JHCommonLibrary also works.

(I need to state again, this issue only appeared in IDEA but not appear if running mvn compile in command line. So it's likey a bug in IDEA)

java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool

When trying to setup spring-session-data-redis, following error is appeared

09-May-2020 14:27:53.522 SEVERE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class core.web.SystemInitListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration#0' defined in file [F:\df_web\target\classes\applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory#0': Failed to introspect bean class [org.springframework.data.redis.connection.jedis.JedisConnectionFactory] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
at org.springframework.context.support.AbstractApplicationContext.__refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:41010)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1696)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:484)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:433)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
at java.security.AccessController.doPrivileged(Native Method)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:324)
at sun.rmi.transport.Transport$1.run(Transport.java:200)
at sun.rmi.transport.Transport$1.run(Transport.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory#0': Failed to introspect bean class [org.springframework.data.redis.connection.jedis.JedisConnectionFactory] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:269)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1118)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1091)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory$DependencyObjectProvider.getObject(DefaultListableBeanFactory.java:1630)
at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.setRedisConnectionFactory(RedisHttpSessionConfiguration.java:209)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.__invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45009)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:45012)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:701)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
… 65 more
Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:247)
… 88 more
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)
… 95 more

pom.xml

Checking source code of jedis on Github, I found version 2.9.0 exactly doesn't define such class. But in version 3.3 it exists.

So change jedis version to 3.3.0 fixed this issue

mvn add jar file to local repository

Did you ever want to put a local jar library file into your deployment jar file, but the library file is not available on public repository (or you don't want to put it on public repository?)

One possible method is to add maven dependency with scope and systemPath property

<dependency>
<groupId>aeszip</groupId>
<artifactId>aeszip</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/WebContent/WEB-INF/lib/aeszip.jar</systemPath>
</dependency>

But using scope and systemPath has a problem, that is when I run mvn package to build a package, the dependency will not be included, so running the application on another machine will always complain NoClassDefFoundError or ClassNotFoundException

So a better method is to use mvn install:install-file plugin

This will add AesZip.jar to your local maven repository. When running mvn package to create package, the dependent libraries will be included into the generated package

Spring disable transaction for method in transactional class

Overview

In Spring @Transactional  annotation can be used to indicate method should be run in transaction.

When @Transactional  is placed on method, this method will be run with transaction. And if it's placed on a class, all methods of this class will run within transaction.

 

In MVC style application, we usually wrap business logic into a separate service class annotated with @Service  and @Transactional , so every method in this class will run within transaction. But how to disable transaction management for a specific single method in class which is annotated with @Transactional ?

 

Solution

Add following code before the method which you want to disable transaction management for.

Above code will tell Spring PlatformTransactionManager that transaction is not supported for this method, and transaction manager won't create a new transaction if no transaction is existed.

And if a current transaction existed, exception will be thrown (An IllegalTransactionStateException with message "Existing transaction found for transaction marked with propagation 'never'").

 

java.lang.NoClassDefFoundError: java/util/Base64

Problem

In the following code, Base64 is used to encode AES encrypted data bytes to plain text for transfering via URL query parameter.

 

But when running this code on my coworker's machine, following exception is thrown

 

It's because that my execution environment is Java 8, but my coworker is using Java 7, and java.util.Base64 is introduced in Java 8.

 

Solution

To fix this issue, we can use Base64 class from Apache Commons Codec.

First add commons codec dependency to maven pom.xml file.

Next import Base64 class from commons-codec

 

Finally replace  Base64.getUrlEncoder().encodeToString to Base64.encodeBase64URLSafeString .

So the code will become like following

 

 

Method.invoke throws java.lang.IllegalArgumentException: wrong number of arguments

Background

There is a private downloadFile method of ZhangyoobaoLeshuaMerchantRegister class, the method signature is like following

A unit test case for this method need be created, in this test case we need pass a null value as parameter to test whether this method can handle null parameter. But a private method cannot be accessed from the instance level. The solution is to use reflection to get the method, and set its accessiblity to public temporarily (by using the Method.setAccessible method).

 

First let's see some code

Above Java code is the unit test case, it uses reflection to get the private method downloadFile of class ZhangyoobaoLeshuaMerchantRegister, and set the access level of this method to public, then call it with parameters. (Note that if setAccessible is not called to set method access level, Method.invoke will throw IllegalAccessException.)

But running this line  result = method.invoke(register, null);  will got following error

It's because the second argument of Method.invoke() is Object...  (An Object[] array), its elements are arguments passed in to the method. Calling Method.invoke() with null as second argument is actually trying to call the method with no arguments, but downloadFile method requires one parameter, so it definitely raises IllegalArgumentException.

 

Solution

To fix this issue, change following line

to

This will pass null as first argument for downloadFile method. new Object[]{null}  is correspondding to the Object...  argument of Method.invoke(), it's expanded like calling downloadFile(null) .

 

Hibernate Query unexpected token: INTERVAL

I'm trying to fetch inventories which is opened at last month, for traditional SQL DATE_SUB function should be used here for date calculation. So the HQL is like following

 

But following error appeared when running it in Hibernate QL Query Consonle in Intellij IDEA

 

Unfortunatelly the INTERVAL keyword is not supported in HQL.

 

Solution

An alternative is to calculate the date in Java and pass the result dates to HQL as parameters. The code is like following

 

Implementation code for DateUtil.getLastMonthFirstDay and DateUtil.getLastMonthLastDay

 

 

 

Spring Cloud Zuul Ribbon custom rule not working

I need a custom Rule which can route requests to different service according to weight option (actually I want to deploy an A/B test service and only small percent of user will hit it), the code is like following

 

And following is application.yml configuration file

 

Adding breakpoints at constructor of ZoneAwareLoadBalancer and DynamicServerListLoadBalancer, then access the Zuul service via browser and I found ZoneAwareLoadBalancer constructor breakpoint is reached first.

Inspect the value clientConfig->properties->NFLoadBalancerClassName, its value is still "com.netflix.loadbalancer.ZoneAwareLoadBalancer", and the passed in rule is not my configured rule as well.

This means my configuration didn't work.

 

Even I changed the class name to an invalid value, there is no error

 

In Spring Cloud documentation, under Customizing the Ribbon Client using properties section, it's saying

Starting with version 1.2.0, Spring Cloud Netflix now supports customizing Ribbon clients using properties to be compatible with the Ribbon documentation.

Checking maven pom file I found it's using 1.1.0

 

Changing them to 1.2.0 and run again, following exception is thrown

 

It's because NFLoadBalancerRuleClassName is aaa, changing it to a valid name and try it again, it works now

 

 

Intellij IDEA plugin development get selected text

To get selected text in a Intellij Platform Plugin project, we can use following code

 

The first line is used to get current editor object

 

Then call getSelectionModel and getSelectedText method to get selected text

 

The last two lines is to to some operation for selected text, it's unrelated to our goal.

 

 

HBase get column qualifiers by column family

To get column qualifiers by column family we can use following code

Above code will outputs all column qualifer names of family map "basicInfo" in table "merchants".

 

And following code shows different ways to retrieve HBase cell value

 

  • Get Cell by Family Map and Column Qualifier

This line of code retrives cell value by family map "merchantNo" and column qualifier "string"

 

  • Get All Cells by Faimily Map

result.getFamilyMap(Bytes.toBytes("merchantNo"))  will return column family map with name "merchantNo", it contains column qualifier name as key and cell value as value.

Above code will iterate the column family map, and outputs each column qualifier and its corresponding cell value.