Heroku Spring Boot Application Start Problem - spring-boot

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.

Related

Spring Boot -Dspring.config.location vs --spring.config.location

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.

Spring Boot/Tomcat, "Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean"

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);
}

Spring Tool Suite: Cannot Execute a Simple Demo because of Spring Boot Build Path ERROR

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.

What is the appropriate driver to load for embedded DynamoDB in a Spring app?

I followed various tutorials to end up with the configuration below. There may also be incorrect configuration causing the problem. When I run the tests, I get:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (the profiles "test" are currently active).
Research has lead me to believe that the appropriate driver is not being loaded and I have to add it to .properties. Is that something included in DynamoDBLocal library? I can't find it in the docs and it seems my only option is to get a 3rd party driver from the web.
Here's are the important parts:
pom.xml:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>1.11.34</version>
</dependency>
<dependency>
<groupId>com.github.derjust</groupId>
<artifactId>spring-data-dynamodb</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>1.10.5.1</version>
</dependency>
DataSourceConfigLocal:
#Configuration
#EnableWebMvc
#EnableDynamoDBRepositories(basePackages="com.cfa.dao")
#Profile({"local", "test"})
public class DataSourceConfigLocal {
#Value("${amazon.dynamodb.endpoint}")
private String amazonDynamoDBEndpoint;
#Value("${amazon.aws.accesskey}")
private String amazonAWSAccessKey;
#Value("${amazon.aws.secretkey}")
private String amazonAWSSecretKey;
#Bean
public AmazonDynamoDB amazonDynamoDB() {
AmazonDynamoDB amazonDynamoDB
= new AmazonDynamoDBClient(amazonAWSCredentials());
if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
}
return amazonDynamoDB;
}
#Bean
public AWSCredentials amazonAWSCredentials() {
return new BasicAWSCredentials(
amazonAWSAccessKey, amazonAWSSecretKey);
}
}
IntegrationTest:
#RunWith(SpringJUnit4ClassRunner.class)
#WebAppConfiguration
#Profile("test")
#TestPropertySource(properties = {
"amazon.dynamodb.endpoint=http://localhost:8000/",
"amazon.aws.accesskey=x",
"amazon.aws.secretkey=x" })
public class OrderRequestRepositoryIntegrationTest {
private DynamoDBMapper dynamoDBMapper;
#Autowired
private AmazonDynamoDB amazonDynamoDB;
#Autowired
OrderRequestDao orderRequestDao;
private static final String STORE_NUMBER = "100";
#Before
public void setup() throws Exception {
dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
CreateTableRequest tableRequest = dynamoDBMapper
.generateCreateTableRequest(OrderRequest.class);
tableRequest.setProvisionedThroughput(
new ProvisionedThroughput(1L, 1L));
amazonDynamoDB.createTable(tableRequest);
dynamoDBMapper.batchDelete(
(List<OrderRequest>)orderRequestDao.findAll());
}
#Test
public void sampleTestCase() {
OrderRequest orderRequest = new OrderRequest(STORE_NUMBER);
orderRequestDao.save(orderRequest);
List<OrderRequest> result
= (List<OrderRequest>) orderRequestDao.findAll();
assertTrue("Not empty", result.size() > 0);
assertTrue("Contains item with expected cost",
result.get(0).getStoreNumber().equals(STORE_NUMBER));
}
}
I am not sure whether you have already referred this. I am adding this as it may help you.
Test using HTTP and without using HTTP
pom file which has the server runner and sql lite
1) Also, use the latest version 1.11.0.1 of the JAR.
2) SQL Lite lib in classpath
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>DynamoDBLocal</artifactId>
<version>${aws.dynamodblocal.version}</version>
</dependency>
<profile>
<id>start-dynamodb-local</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${exec.maven.plugin.version}</version>
<executions>
<execution>
<phase>initialize</phase>
<configuration>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<classpath/>
<argument>-Dsqlite4java.library.path=${basedir}/target/dependencies</argument>
<argument>com.amazonaws.services.dynamodbv2.local.main.ServerRunner</argument>
<argument>-inMemory</argument>
<argument>-port</argument>
<argument>${dynamodb-local.port}</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

#Configuration inside jar not getting loaded

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>

Resources