We #Configuration file inside our project and we are adding one "xyz.jar" in class-path as maven dependency having another #Configuration file.
We are using one autowired bean from "xyz.jar" and when we run our project getting following error for this bean.
"No qualifying bean of type"
We are using Spring-boot, spring-amqp and maven.
We have tried one solution to this by adding following lines to our configuration class
#AutoConfigurationPackage
#Import(value = XYZConfiguration.class) //another #Configuration class from jar
This is working but not working when we run this jar on docker container.
Could you please help.
//Spring boot application class
#SpringBootApplication
public class Application implements CommandLineRunner
{
private static final Logger logger = LoggerFactory.getLogger(Application.class);
public static void main(String[] args)
{
logger.debug(" Application starting ....");
SpringApplication.run(VApplication.class, args);
}
#Override
public void run(String... arg0)
{
logger.debug("Application is running....");
}
}
// Configuration class
#Configuration
#AutoConfigurationPackage
#Import(value = XYZConfiguration.class)
public class VRMQConfig
{
//Bean Configuration
}
Docker File:
FROM anapsix/alpine-java
MAINTAINER MyService
COPY myservice.jar /home/myservice.jar
CMD ["java","-jar","/home/myservice.jar
POM file :
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
Related
What's difference between -Dspring.config.location and --spring.config.location for running Spring Boot in command line?
I saw more than one tutorials and stackoverflow answers running command in this way:
java -jar test.jar --spring.config.location=classpath:test/application.properties
However, no matter how I tried, I failed to load the properties file whereas another way succeeded:
java -jar -Dspring.config.location=classpath:test/application.properties test.jar
I am confused with these two settings.
My project is using:
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
...
</plugin>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>3.0.0</version>
</dependency>
The way I tested:
#SpringBootApplication
public class Test implements CommandLineRunner {
#Autowired
private ConfigurableApplicationContext context;
public static void main(String[] args) throws InterruptedException {
SpringApplication.run(Test.class);
}
#Override
public void run(String... args) throws Exception {
System.out.println(context.getId());
}
}
The first way output is "application". The second way can read my value from my properties file. There are no any errors for both.
Thanks.
I created a Java Spring Boot Application.
I want to publish on Heroku but I have a problem.
I used ModelMapper on my project.
I published my app and then I wrote heroku ps:scale web=1
But my exception is:
2020-12-06T22:30:02.017002+00:00 app[web.1]: Error: Unable to initialize main class com.faksoy.stocktracking.StockTrackingApplication
2020-12-06T22:30:02.017004+00:00 app[web.1]: Caused by: java.lang.NoClassDefFoundError: org/modelmapper/ModelMapper
My StockTrackingApplication class is:
#SpringBootApplication
public class StockTrackingApplication {
public static void main(String[] args) {
SpringApplication.run(StockTrackingApplication.class, args);
}
#Bean
public ModelMapper getModelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
return modelMapper;
}
}
And last my Heroku JDK version is 15.0.1.
Thanks
I found my problem.
My problem is my pom.xml is not correct for maven plugin.
I changed my pom.xml like this
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
and deleted other maven tools.
My problem solved.
I know there are others questions on this, and I have checked them, but none solves my issue.
Very simple POM, ready:ing my app for use with an embedded web server, simply so I can start it up...
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Very easy Boot class:
#SpringBootApplication
#RestController
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringApplication.class, args);
}
#RequestMapping(value = "/")
public String hello() {
return "Hello World from Tomcat";
}
}
Tried:
Extend the SpringBootServletInitializer class.
Update spring-boot-starter-parent to the latest (2.1.3).
Setting spring.profiles.active=default in application.properties.
Manually inject TomcatWebServerFactory (ugh...) in #Configuration
class.
Run mvn dependency:purge-local-repository and removed /repository dir
from my .m2 dir, fetching all again with mvn clean install -U.
ETC... I do not see why it is not working, I though Spring Boot was supposed to be easy.
Unable to start ServletWebServerApplicationContext due to missing
ServletWebServerFactory bean.
First argument for SpringApplication.run() is expected to be the primary configuration class of your application. In your case it is SpringBootApp.class and not SpringApplication.class. Following would be the correct configuration for your Spring Boot application.
public static void main(String[] args)
{
SpringApplication.run(SpringBootApp.class, args);
}
I am learning Spring Framework and the first goal for me is to return an Version object using a built in serializer.
public class Version {
private int build;
private String releaseType;
public Version(int build, String releaseType) {
this.build = build;
this.releaseType = releaseType;
}
public int getBuild() {
return build;
}
public String getReleaseType() {
return releaseType;
}
public void setBuild(int build) {
this.build = build;
}
public void setReleaseType(String releaseType) {
this.releaseType = releaseType;
}
}
And my root class (I called it Kernel), would love to stay with application configuration using one class
#EnableWebMvc
#Configuration
public class Kernel extends AbstractDispatcherServletInitializer implements WebMvcConfigurer {
#Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext();
annotationConfigWebApplicationContext.register(VersionController.class);
return annotationConfigWebApplicationContext;
}
#Override
protected String[] getServletMappings() {
return new String[] { "/*" };
}
#Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
}
}
The controller
#RestController
public class VersionController {
#RequestMapping("/version")
#ResponseBody
public Version getVersion() {
return new Version(192837, "DEV");
}
}
I am trying to go as simple as I can, I don't have any XML file in my project as I want to go fully Annotation & Java mode as long as I can.
I never really liked the XML driven concept in Spring Framework as most of time the content of these XML files looks like exposed programmer garbage that nobody but owner would understand how to setup. What is the point of exposing configuration for response serializers as deployment worker will have no clue what is that.
The error I got is:
HTTP Status 500 – Internal Server Error, No converter found for return value of type
I suspect that the Jackson is not called because Spring does not know that he is expected to use it on Version object, but I have no idea how to force Spring to do it.
My pom.xml just for sure (using Tomcat9 as web server)
<?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>pl.protean.league-craft</groupId>
<artifactId>league-craft</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<warName>lc</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Kernel</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I solved my issue thanks to this answer
https://stackoverflow.com/a/10650452/2010246
A
#Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter());
}
Will not work just like that even if I am overriding this method in AbstractDispatcherServletInitializer
Instead of that I had to create separate class for MVC configuration
package configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import java.util.List;
#Configuration
public class Mvc extends WebMvcConfigurationSupport {
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(converter());
addDefaultHttpMessageConverters(converters);
}
#Bean
MappingJackson2HttpMessageConverter converter() {
return new MappingJackson2HttpMessageConverter();
}
}
Now it loads correctly and I can simply return the class like I wanted to
I guess the core problem was there were no class that is annotated as #Configuration and extends the WebMvcConfigurationSupport parent
The Error is as follows:
Description Resource Path Location Type The project was not built
since its build path is incomplete. Cannot find the class file for
org.springframework.context.ConfigurableApplicationContext. Fix the
build path then try building this project springbootdemo Unknown Java
Problem
2 ERROR items:
Description Resource Path Location Type The type
org.springframework.context.ConfigurableApplicationContext cannot be
resolved. It is indirectly referenced from required .class
files MainActivity.java /springbootdemo/src/main/java/springbootdemo line
10 Java Problem
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainActivity {
public MainActivity() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
//this is where the ERROR happens beginning at
// "SpringApplication.run(MainActivity.class, args);"
ApplicationContext ctx = SpringApplication.run(MainActivity.class, args);
}
}
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>labs.noogui.springbootquickstart</groupId>
<artifactId>course-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot course-api</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<java.version>1.8</java.version>
</properties>
</project>
Project Structure:
**Spring Tool Suite**
Version: 3.9.4.RELEASE
Build Id: 201804120921
Platform: Eclipse Oxygen.3a (4.7.3a)
I'm following the Spring Boot tutorial from Edureka!
How do I fix this build path error? I mean, this is like a spring boot hello world and I'm already running into errors?
Just use below code,
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class MainActivity {
public static void main(String[] args) {
SpringApplication.run(MainActivity.class, args);
}
}
or if you want to use ApplicationContext to start the application then use below line but both are almost same.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
#SpringBootApplication
public class MainActivity {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(MainActivity.class, args);
}
}
You are using ApplicationContext from org.apache.catalina.core.
Import and use this org.springframework.context.ApplicationContext instead.
Your IDE is showing compile errors. Error message should probably lead you to the right direction.
Thanks for the folks who tried to help but I found out that the problem is with maven's 'corrupted' repository (again). So as always, deleting the .m2/repository and updating your maven project resolves the issue.
Related post in this github forum. This is a recurring issue with maven.