Spring-Boot project executes in IDE but not when in a JAR - java

The project current runs fine via IntelliJ. Its built with Java 11, spring-boot 2.4.3, and gradle 6.8.3
The error as I read it states that it cannot find a 'RestDriver'( which is annotated as #RestController) nor the interface 'DriverService' which has no annotations on it. That class uses a a class annotated with #Service 'DriverServiceImp'.
Its as if I will have to state in main where these classes are because it cannot find them.
2021-10-21 12:04:45 [main] ERROR o.s.boot.SpringApplication at 856 - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'restDriver':
Unsatisfied dependency expressed through field 'driverService';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'DriverService': Lookup method resolution failed;
nested exception is java.lang.IllegalStateException:
Failed to introspect Class [com.xxx.wfmDriverservice.service.DriverServiceImp] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader#5c7fa833]

Related

spring.cloud.kubernetes.discovery-server-url must be specified and a valid URL

I'm updating a service's dependencies from Spring Boot 2.3.12.RELEASE to 2.7.0 and Spring Cloud from Hoxton.Release to 2021.0.3
Upon doing this, I'm starting to get kubernetes errors that I didn't see with the previous dependency versions.
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'compositeDiscoveryClient' defined in class path resource [org/springframework/cloud/client/discovery/composite/CompositeDiscoveryClientAutoConfiguration.class]: Unsatisfied dependency expressed through method 'compositeDiscoveryClient' parameter 0;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'kubernetesDiscoveryClient' defined in class path resource [org/springframework/cloud/kubernetes/discovery/KubernetesDiscoveryClientAutoConfiguration$Servlet.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.client.discovery.DiscoveryClient]: Factory method 'kubernetesDiscoveryClient' threw exception;
nested exception is org.springframework.cloud.kubernetes.discovery.DiscoveryServerUrlInvalidException: spring.cloud.kubernetes.discovery-server-url must be specified and a valid URL.
I've read a few tutorials about using the #EnableDiscoveryClient annotation in the application as well as autowiring the DiscoveryClient in the controllers, including the Baeldung tutorial. However, I have yet to see anything mention a discovery-server-url.
Where do I set this value, or how to I tell Spring and Kubernetes what to look for?
Notice that "spring.cloud.kubernetes.discovery-server-url" has a spelling error in Springs error message. The parameter name is spring.cloud.kubernetes.discovery.discovery-server-url.
It can be set anywhere in Springs external configuration (.properties, .yaml). Regarding K8s, the quickest way is perhaps the args in your deployment.yaml:
template:
metadata:
labels:
...
spec:
containers:
- name: ...
image: ...
command: ["java"]
args: ["-Dspring.profiles.active=kubernetes", "-Dspring.cloud.kubernetes.discovery.discovery-server-url=http://spring-cloud-kubernetes-discoveryserver.default.svc.cluster.local", "-Dspring.cloud.kubernetes.discovery.enabled=true", "org.springframework.boot.loader.JarLauncher"]
ports:
...

Another service JAR is creating Spring's autoconfiguration (Error creating bean with name 'mongoTemplate')

I have one Java spring boot application (call it utility service) and many other Java spring boot applications (call them as clients).
Both (utility service and clients) use Spring's autoconfiguration. When I am trying to put JAR of utility service into a client, after building the client, it is throwing error at bootrun. The error is:
Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.mongodb.MongoDbFactory]: :
Error creating bean with name 'mongoDbFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type [com.mongodb.MongoClient]: :
Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.mongodb.MongoClient]:
Factory method 'mongo' threw exception;
nested exception is java.lang.NoSuchMethodError:
org.bson.io.BasicOutputBuffer.<init>(I)V;
I can fix this problem by applying #EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) in the client's Application.java.
But I have many such clients and I don't want to modify client's code (except it's build.gradle).
Is there any way from utility service to modify and achieve above?
You can try to exclude the class when you include the dependency in any of your clients. Once you exclude the mongoTemplate configuration class it should not appear in your classpath while building the client service code.
Hope this helps.

Why api works fine locally but remotely shows unsatisfied dependency?

I have a rest api in Spring Framework with Java, it works with amazon aws services, using the "aws-java-sdk-1.5.0.jar" file, working very well. But, I have to implement Amazon SES and that version of that jar doesn't support it, for that reason I downloaded a most recent version "aws-java-sdk-1.11.269.jar", I replaced it and deleted the previous version, and rebuild the project. It works fine again but only locally. Remotely the WAR file cannot be deployed, It shows an error about Unsatisfied dependency exception, but remember locally deploys and works fine. Do you know what I'm doing wrong?
13-Sep-2019 20:43:16.892 INFO [main] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
13-Sep-2019 20:43:16.909 INFO [main] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
13-Sep-2019 20:43:19.363 SEVERE [main] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class [org.springframework.web.context.ContextLoaderListener]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'settingsRestFacade': Unsatisfied dependency expressed through field 'fileStorageServices'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fileStorageServices' defined in com.desafioguerreros.SpringConfiguration: Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.desafioguerreros.infrastructure.storage.FileStorageServices]: Factory method 'fileStorageServices' threw exception; nested exception is
java.lang.NoSuchMethodError: org.apache.http.conn.ssl.SSLConnectionSocketFactory.<init>(Ljavax/net/ssl/SSLContext;Ljavax/net/ssl/HostnameVerifier;)V
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1348)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
...

Spring Boot SessionFactory instantiation failed but only when containerised

I have containerised a spring boot application. It is contained in a jar file. when I call the jar file separately It is working but when I run it through docker-compose up it yields the following error:
ConfigServletWebServerApplicationContext : Exception encountered
during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'crawlerController': Unsatisfied
dependency expressed through field 'campaignservice'; nested exception
is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'campaignService': Unsatisfied
dependency expressed through field 'campaignrepository'; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'campaignRepository': Cannot resolve
reference to bean
'org.springframework.data.neo4j.transaction.SharedSessionCreator#0'
while setting bean property 'session'; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.data.neo4j.transaction.SharedSessionCreator#0':
Cannot resolve reference to bean 'sessionFactory' while setting
constructor argument; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'sessionFactory' defined in class path
resource
[org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]:
Bean instantiation via factory method failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method
'sessionFactory' threw exception; nested exception is
java.lang.TypeNotPresentException: Type javafx.util.Pair not present
Given that the jar works when invoked separately I assume that all of the dependencies are contained in the classpath. so I do not understand why it does not work when called through Docker
the dockerFile is :
FROM openjdk:8-jre
VOLUME /tmp
ADD target/SpringNeoServer.jar /SpringNeoServer.jar
EXPOSE 7864
ENTRYPOINT ["java", "-jar", "SpringNeoServer.jar"]
JavaFX is not included in this version of openjdk.
Instructions to add it can be found here.

REST application runs in Eclipse but not as an executable jar. NoClassDefFoundError: javax/xml/bind/annotation/XmlElement

I have written REST resources using Jersey annotations. To make that work in Spring Boot I added a JerseyConfiguration class:
#Configuration
#ApplicationPath("/api")
public class JerseyConfiguration extends ResourceConfig {
public JerseyConfiguration() { }
#PostConstruct
public void setUp() {
register(AdminController.class);
register(AdminResource.class);
register(GenericExceptionMapper.class);
}
}
This works fine when running the application in Eclipse. When I create and run the jar-file I get the following error:
12:45:15.311 [main] ERROR o.s.boot.SpringApplication : Application startup failed org.springframework.context.ApplicationContextException: Unable to start embedded container;
nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration': Bean instantiation via constructor failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration$$EnhancerBySpringCGLIB$$d46b8aee]: Constructor threw exception;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceConfigCustomizer' defined in class path resource [org/springframework/boot/autoconfigure/jersey/JerseyAutoCon figuration$JacksonResourceConfigCustomizer.class]: Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer]: Factory method 'resourceConfigCustomizer' threw exception;
nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlElement
Can anybody tell me why this happens? As far as I know the javax/xml/bind/annotation is part of the Java 8 rt.jar
I am using Spring Boot 1.5.9.RELEASE, Java 8
I thought I started the jar using Java 8 SDK, but it was really the Java 9 JRE. So problem does not appear when starting the jar with the Java 8 SDK java.exe. Starting the jar with the Java 8 SDK has solved the problem.

Categories

Resources