Netbeans 8 won't reload static Thymeleaf files - spring

I am using Spring Boot and Thymeleaf via Maven. I can't seem to get Netbeans to automatically re-deploy any of my Thymeleaf template files when I make changes. In order to see the changes I need to do a full clean/build/run. This takes way too long.
The templates are in src/main/resources/templates. I have an application.properties file in src/main/resources/ with spring.thymeleaf.cache=false and spring.template.cache=false.
I have "Compile on save", "Copy resources on save" and "Deploy on save" turned on in the project settings.
My maven build produces a war file that Netbeans deploys to Tomcat and I am using the annotation #EnableAutoConfiguration.
Netbeans does hot deploy changes to the Java classes but not for any of the static files in src/main/resources/.
Software in use:
Mac OS X 10.9.4
Java 1.8
Netbeans 8.0.1
Tomcat 8.0.12
Spring Boot 1.1.7
Thymeleaf 2.1.3 (via Spring Boot)
Any guidance is much appreciated.

An option would be to look into configuring Thymeleaf's FileTemplateResolver
To do that with Spring Boot, define a bean implementing the ITemplateResolver interface with the name defaultTemplateResolver, when present, Spring Boot would take it instead of its default, here is how that would be done, and assuming you have component scanning active so this configuration class will be picked up automatically:
#Configuration
public class ThymeleafConfiguration {
#Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(".html");
resolver.setPrefix("path/to/your/templates");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
return resolver;
}
}
The prefix should be a relative path that when added to your runtime working directory (cwd), would resolve to the templates directory. If you are unsure, set that to the full absolute path, but then there would be no point of the above bean. Since setting the spring.thymeleaf.prefix property to an absolute path would probably have the same effect.

Have been looking for a solution to my eclipse+thymeleaf+sprint boot reloading templates dynamically for a log while....
Finally I found this question here and spring.thymeleaf.cache=false and spring.template.cache=false fixed my issue.

Besides setting the Thymeleaf views as non-cacheable by ie. spring.thymeleaf.cache=false in your application.properties,
try explicitly defining the resource directory in your pom.xml:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
...
</build>

To deal with that, the spring-boot-maven-plugin in the pom.xml should seems like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
And add this to your application properties:
spring.thymeleaf.cache=false
It usually works to Spring beans too.

Just to say this works nicely for me using an external instance of Tomcat:
Run Tomcat with JRebel or Spring Loaded javaagent as a VM option
Turn off "Compile on save", "Copy resources on save" and "Deploy on save"
Add a custom action in Netbeans that executes the compile goal
Run that when you want to see an update
http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions
https://github.com/spring-projects/spring-loaded
https://zeroturnaround.com/software/jrebel/quickstart/standalone/
Or you can you use the embedded tomcat with spring-boot-maven-plugin and Spring Loaded instead, then you won't need the compile action:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html

I had this problem too. I notice Netbeans reload automaticaly webpages that are in
/src/main/webapp/
You have to move all your templates from /src/main/resources/templates to this directory.
Also you have to change the spring boot property on application.properties file:
spring.thymeleaf.prefix=templates/
That works for me

I had this same issue on Netbeans 8.0.2 and Windows. I was building a WAR to be deployed to Tomcat, but I wanted to try out Spring Boot. It looks like newer versions of Netbeans might resolve this with the Spring Boot plugin or using Eclipse. It seemed nutty to swap IDEs over something tiny like this. I tried all of the suggestions I could find; spring loaded, caching properties, extending the TemplateResolver...I couldn't get any of them to work. I finally stumbled on this blog and following these instructions solved my issue.

Related

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

I am working on a Spring Boot Batch example with MongoDB and I have already started the mongod server.
When I launch my application, I am getting the error below.
Any pointers for this issue?
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
application.properties:
# Mongo database URI. Cannot be set with host, port and credentials.
spring.data.mongodb.uri=mongodb://localhost/test
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I have started mongod with the following output:
C:\Users\pc>mongod
2018-07-07T14:39:39.223+0530 I JOURNAL [initandlisten] journal dir=C:\data\db\journal
2018-07-07T14:39:39.230+0530 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2018-07-07T14:39:39.478+0530 I JOURNAL [durability] Durability thread started
2018-07-07T14:39:39.589+0530 I CONTROL [initandlisten] MongoDB starting : pid=11992 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-NQ639DU
2018-07-07T14:39:39.589+0530 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-07-07T14:39:39.591+0530 I CONTROL [initandlisten] db version v3.0.5
2018-07-07T14:39:39.592+0530 I CONTROL [initandlisten] git version: 8bc4ae20708dbb493cb09338d9e7be6698e4a3a3
2018-07-07T14:39:39.592+0530 I CONTROL [initandlisten] build info: windows sys.getwindowsversion(major=6, minor=1, build=7601, platform=2, service_pack='Service Pack 1') BOOST_LIB_VERSION=1_49
2018-07-07T14:39:39.592+0530 I CONTROL [initandlisten] allocator: tcmalloc
2018-07-07T14:39:39.593+0530 I CONTROL [initandlisten] options: {}
2018-07-07T14:39:39.595+0530 I JOURNAL [journal writer] Journal writer thread started
2018-07-07T14:39:40.485+0530 I NETWORK [initandlisten] waiting for connections on port 27017
2018-07-07T14:40:39.140+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:51340 #1 (1 connection now open)
2018-07-07T14:40:41.663+0530 I NETWORK [conn1] end connection 127.0.0.1:51340 (0 connections now open)
2018-07-07T14:45:12.421+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:51578 #2 (1 connection now open)
2018-07-07T14:45:12.870+0530 I NETWORK [conn2] end connection 127.0.0.1:51578 (0 connections now open)
2018-07-07T14:46:21.734+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:51591 #3 (1 connection now open)
2018-07-07T14:46:22.041+0530 I NETWORK [conn3] end connection 127.0.0.1:51591 (0 connections now open)
2018-07-07T14:57:47.523+0530 I NETWORK [initandlisten] connection accepted from 127.0.0.1:52534 #4 (1 connection now open)
2018-07-07T14:57:47.910+0530 I NETWORK [conn4] end connection 127.0.0.1:52534 (0 connections now open)
Just add : #SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
works for me.
I was getting same error I tried with #EnableAutoConfiguration(exclude=...) didn't work.
For those that are wondering where to add #SpringBootApplication(exclude = {DataSourceAutoConfiguration.class }), as it has been asked by user as well. You need to add it to the main Application class which is under src>main>java. By default it is set to #SpringBootApplication
check your application.properties
changing
spring.datasource.driverClassName=com.mysql.jdbc.Driver
to
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
worked for me. Full config:
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
Your problem is the dependency of spring batch spring-boot-starter-batch that has a spring-boot-starter-jdbc transitive maven dependency.
Spring Batch is a framework for building reliable and fault tolerance enterprise batch jobs. It supports many features like restarting a failed batch, recording the status of the batch execution and so on. In order to achieve that Spring Batch uses a database schema to store the status of the registered jobs, the auto-configuration already provides you the basic configuration of the required data source and it is this configuration that requires the relational database configuration.
To solve this you must include some database driver like mysql, h2, etc. to configure the url.
Update:
Just for getting start you can configure your application.yml like below:
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:localhost;DB_CLOSE_ON_EXIT=FALSE
username: admin
password:
and of course in your pom.xml include the h2 dirver like this:
<?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>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
....
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
....
</dependencies>
...
</project>
The motivation, because you can not use mongo for this purpose, is that the usage of mongo is provided only for item readers and writers and not for managing the internal database of Spring Batch that is an internal schema, not a business schema. The query is plain SQL query and the internal abstraction relies on a relational database. It is necessary to have a database with ACID capability because every batch reads and writes a chunk of work and saves that information in order to restart the job. A NoSql solution is not suitable for this.
At the end you have configured a relational database in order to prepare Spring Batch for internal capability, the internal abstraction does not rely on mongo only on jdbc. Then mongo can be used but for the business side of the batch via item reader/writer.
I hope that this can help you to clear your doubts.
Root Cause
The JPA (Java persistence API) is a java specification for ORM (Object-Relational Mapping) tools. The spring-boot-starter-data-jpa dependency enables ORM in the context of the spring boot framework.
The JPA auto configuration feature of the spring boot application attempts to establish database connection using JPA Datasource. The JPA DataSource bean requires database driver to connect to a database.
The database driver should be available as a dependency in the pom.xml file. For the external databases such as Oracle, SQL Server, MySql, DB2, Postgres, MongoDB etc requires the database JDBC connection properties to establish the connection.
You need to configure the database driver and the JDBC connection properties to fix this exception Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a suitable driver class.
application.properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
application.yaml
spring:
autoconfigure:
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
By Programming
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
Not to the point of the question (can be related though), but, if you bootstrap a new project and wondering why do you get the same error, it may come from the artifactId of spring-boot-starter-data-jpa in the dependency section. I gave the dependency below. You will need to define the database to get rid of this.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Excluding the DataSourceAutoConfiguration.class worked for me:
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
I had the same issue resolved by add <scope>provided</scope>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
Source:
https://github.com/spring-projects/spring-boot/issues/13796#issuecomment-413313346
This link helped.
Spring Boot auto-configuration tries to configure beans
automatically based on the dependencies added to the classpath. And
because we have a JPA dependency (spring-data-starter-jpa) on our classpath, it tries to configure it.
The problem: Spring boot doesn't have the all the info needed to configure the JPA data source i.e. the JDBC connection properties.
Solutions:
provide the JDBC connection properties (best)
postpone supplying connection properties by excluding some AutoConfig classes (temporary - should be removed eventually)
The above link excludes the DataSourceAutoConfiguration.class with
#SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
But this didn't work for me. I instead, had to exclude 2 AutoConfig classes:
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
It can be that your resources directory is not added to classpath when creating a project via Spring Initializr. So your application is never loading the application.properties file that you have configured.
To make a quick test if this is the case, add the following to your application.properties file:
server.port=8081
Now when running your application you should see in the spring boot console output something like this:
INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): **8081** (http) with context path ''
If your port is still default 8080 and not changed to 8081, your application.properties files is obviously not loading.
You can also check if your application runs with gradle bootRun from command line. Which most likely will be work.
Solution:
Close IntelliJ, then inside your project folder delete the ".idea" folder
Reimport your project to IntelliJ like following: "Import Project" -> "select ONLY your build.gradle file to import". (IntelliJ will automatically grab the rest)
build and run your application again
See official answer by IntelliJ Support:
IDEA-221673
The following worked for 2021 spring-boot release 2.5.0
If you have as minimum these entries in your application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/db
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
And these dependencies in your pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
You should not have this error:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
It was the IDE in my case
No matter if you are using eclipse or intellij, application must run over linux in real environments. So to validate if is an IDE problem, run your app using shell
mvn spring-boot:run
If it starts without error, the problem is in your IDE
Eclipse
Eclipse IDE for Enterprise Java and Web Developers
Version: 2021-03 (4.19.0)
Build id: 20210312-0638
On my case I was running the project with right click on the classic Application.java inside of spring boot project , then run as java application
After hours of researching, the solution was:
right click on the root spring boot project, then run as java application. Eclipse shows me several class with main methods. I choose my Application.java and then run
Long Explanation
If you check the exact method error DataSourceProperties.determineDriverClassName you will see that just driverClassName or dirver-class-name and url is required.
“Failed to configure a DataSource” error. First, we fixed the issue by defining the data source. Next, we discussed how to work around the issue without configuring the data source at all.
https://www.baeldung.com/spring-boot-failed-to-configure-data-source
I have added this annotation on the main class of my spring boot application and everything is working perfectly
#SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
I faced the same issue in my code, adding this code in Application.java file helped me out-
#SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class})
For spring boot version 2.X.X below configuration worked for me.
spring.datasource.url=jdbc:mysql://localhost:3306/rest
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
Old jdbc driver is deprecated. The new one is mentioned on above configuration.
Please use the same and restart the project.
It can be because you have jpa dependendencies and plugins...
just comment it if not use(build.gradle or pom file)
e. g.
// kotlin("plugin.jpa") version "1.3.61"
// implementation("org.springframework.boot:spring-boot-starter-data-jpa")
An optiona Solution if already tested all last answers
If you have an error like title
And validated .properties connection string is correct.
Then add this code block about maven plugin in your .pom file
...
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
...
And Update project. Works for me!!!
I hope this help you to.
This steps you the found in this link page.
Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources
https://exerror.com/failed-to-execute-goal-org-apache-maven-pluginsmaven-resources-plugin3-2-0resources/
And here:
Maven clean install: Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources
It's happening because the #valerio-vaudi said.
Your problem is the dependency of spring batch
spring-boot-starter-batch that has a spring-boot-starter-jdbc
transitive maven dependency.
But you can resolve it set the primary datasource with your configuration
#Primary
#Bean(name = "dataSource")
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource getDataSource() {
return DataSourceBuilder.create().build();
}
#Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
If you're using Gradle, rebuild Gradle can solve this problem.
Add this annotation in main java file
#EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
So, I am had with similar problem, and this link helpfull
https://www.yawintutor.com/failed-to-configure-a-datasource-failed-to-determine-a-suitable-driver-class/
What I understood is that preset of project need to have a "RDBMS Database" and a "In Memory Database"
RDBMS Database
mysql
Postgres
oracle
SQL server
In Memory Database
H2 Database
HSQL Database
Derby Database
So, when I selected this preset, all worked great
If datasource is defined in application.resources, make sure it is locate right under src/main and add it to the build path.
If you have a JPA dependency in your pom.xml then just remove it. This solution worked for me.
For me the resource folder was getting excluded on a maven update/build. I went to Build Path>Source and found that src/main/resources have "Excluded **". I removed that entry (Clicked on Excluded **>Remove>Apply and Close).
Then it worked fine.
If you are using YAML for configuration, then it might be indentation problem. Thoroughly check the YAML files.
It simply means you have downloaded a spring starter code with database dependency without configuring your database, So it doesn't know how to connect. For Spring boot version 2.18 do the following steps to fix it.
Create a database for the driver you have downloaded ie mysql/mongo etc.
In your applications.properties file add the db connection info. Sample is given for mysql if your db is mongo change it for mongo.
spring.datasource.url=jdbc:mysql://localhost:3306/db_name_that_you_created
spring.datasource.username=your_db_username_here
spring.datasource.password=your_db_pass_here
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
Reboot the server it will be running.
Inside pom.xml file always keep the updated spring framework version.
I had created a project with spring framework version 2.5.5 and it was working fine that time. After a couple of months, I found that it is not working correctly. Then I put the latest version of the spring framework. Then it works.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>Updated version</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Check spring profile also, by default it goes for 'default' profile, if your application properties have different profiles like the test, prod, etc then you need to set up it. for eclipse set environment variable as
name=spring.profiles.default, value=test
I had the same issue and tried all suggestions above, but didnt work out. I am posting my answer for furture readers. Before it was working fine but somehow it apeared again. I resolved this issue by removing some unnecessary plugnins and depencies from pom.xml
First of all, I changed default packaging type to jar (Spring Boot Initializer gives pom in packaging)
<packaging>jar</packaging>
I added unintentional some plugins:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
<webXml>target/web.xml</webXml>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
I hope my answer will help someone.
This one worked for me, for MySQL:
(Application properties)
spring.datasource.url=jdbc:mysql://localhost:3306/db?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&
useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
I removed an obsolete dependency on mybatis in the pom.xml to get mine running.

intelij spring boot hot swap causes context reload

I am using Intellij 2017.2 with Spring Boot 1.5.4
When I recompile my current class with ctr+shift+F9, instead of the IDE doing a bytecode hotswap, the spring container gets reloaded.
On top of that, after the reload my RestConroller no longer works
I have tried adding / removing from my pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
please advise
I am not sure if this is a spring boot bug as of the new version, but I ended up disabling manually the hot reload via properties:
This is done by setting the following in your application.properties:
spring.devtools.restart.enabled=false
The official doc:
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-restart-disable

Utility Jar using Spring Boot

Created a custom jar using spring boot and this is using for CRUD operations on a database table.The purpose is used to make this a utility jar, so that other services or applications can use this jar for any operations on that table. Following are the steps I followed:
1). Added this jar entry in pom.xml of a REST SERVICE and build got successfully.
2). Autowired the service class of Utility jar inside the controller of REST SERVICE.
But when I started the REST SERVICE (service is developed on spring boot), I got the error as 'the ****controller can require a bean of type *****serviceUtility. Consider defining a bean of type in your configuration'. But I am not able to see any configuration class inside the rest service and it is using application.yml for datasource related things. I am new to Spring and Spring Boot. Could any one guide me how to configure the utility jar in external services.
It is hard to tell without source code, but Spring Boot jars do not by default work as utility jars. The packaging is different. To get a Spring Boot jar to work as a utility jar within another project, you'll want to configure the build plugin like this :
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
Which is described here:
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/repackage-classifier.html
If you are using spring boot , by default , the utility jar will not move all its dependencies .
You can add all the dependencies in the pom.xml for the Rest service .
Else you end up packaging some core modules twice increasing the size of your jar .

Spring boot, JSP file as view not loading when run in IntelliJ

I've created a simple Spring Boot Web Application in intelliJ. I've placed a simple .jsp file in the /src/main/resources/templates/ folder which contains some basic HTML.
I'm trying to return this in a controller but I'm getting this error;
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Sep 09 10:37:46 BST 2016
There was an unexpected error (type=Not Found, status=404).
No message available
I'm assuming that Spring is unable to find the .jsp file, but there's no other errors appearing in the console to give me any further information.
Here's my simple controller;
#Controller
#RequestMapping("/test")
public class TestController {
#RequestMapping("")
public ModelAndView index() {
return new ModelAndView("test");
}
}
I've included the following in my application.properties file;
spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp
My POM file includes the following dependencies;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
I'm using Spring Boot with embedded tomcat.
I've tried changing the path to the views inside application.properties to;
classpath:/templates/ but that also didn't make any difference.
Finally, here is the structure of my project;
When running the application, I'm just using the 'Run' option in IntelliJ.
I have recently experienced the same situation with below dependency:-
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
If I change the "scope" to "default", run the application with Intellij, it works fine.
But if I want to keep the "scope" as "provided", I have to use the command line (terminal) and execute the following:-
mvn clean spring-boot:run
It works for me.
Setting working directory helped me --- by default, it was empty.
Edit your configuration for Spring boot application in Idea.
Set up the working directory, like it's done on a screenshot below:
If I make the tomcat-embed-jasper dependency default scope (not marked "provided") then everything works ... with spring boot 1.5.2 and idea 2017.1. Otherwise, it's kind of difficult to change this - if you change it in the IDEA project structure, it just gets lost the next time it updates the project from maven or gradle. I haven't figured out a way to otherwise make it work.
Things are further complicated if you use the Spring runner in IDEA -- though I recommend that regardless. It makes things nicer when IDEA fully knows your project is Spring.
There is a special hint for IntelliJ user in the Spring Boot reference documentation Chapter 27.1.7 Template engines:
IntelliJ IDEA orders the classpath differently depending on how you
run your application. Running your application in the IDE via its main
method will result in a different ordering to when you run your
application using Maven or Gradle or from its packaged jar. This can
cause Spring Boot to fail to find the templates on the classpath. If
you’re affected by this problem you can reorder the classpath in the
IDE to place the module’s classes and resources first. Alternatively,
you can configure the template prefix to search every templates
directory on the classpath: classpath*:/templates/.
After banging head here and there I figured out how to fix this problem. keep in mind I am using Windows 10 machine and IntelliJ version is 2021.2.2. Here are the steps:
In IntelliJ IDE Click on Run -> Edit Configurations...
In the Working Directory text field put %MODULE_WORKING_DIR%, I guess for linux it may be $MODULE_WORKING_DIR
Click Apply button
Click OK button
Run your Application
It will work.
A bit late but this worked for me.
Add your .jsp files at this path:
src/main/resources/META-INF/resources/
And if you want to configure the configure.properties file, add these lines in it:
spring.mvc.view.prefix= /WEB-INF/views/
spring.mvc.view.suffix= .jsp
Credit : https://www.logicbig.com/tutorials/spring-framework/spring-boot/boot-serve-dynamic.html
The following will make the application work with IntelliJ:
Under main, create the folder structure webapp/WEB-INF/jsps/ - the last part of the folder structure can be named anything, but it is where the jsps will reside.
Remove the properties:
spring.mvc.view.prefix = /templates/
spring.mvc.view.suffix = .jsp
from your application.properties file, and in a Configuration class explicitly create an InternalResourceViewResolver bean, and set these properties there.
This is what your Configuration class will look like:
#Configuration
#EnableWebMvc
public class WebMvcConfig {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsps/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Give that a try - it should work in IntelliJ
After many trials, it worked for me. Its the combination of 2 steps.
1) Maven packaging must be set to 'war', then only it puts everything under 'webapp' into the target war file.
If packaging is 'jar', its omitting the 'webapp' directory.
2) Running spring boot main class from IntelliJ is not working.
Run the application from command prompt using command: 'mvn spring-boot:run'
In IntelliJ you CAN NOT put provided under tomcat-embed-jasper!
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
After many trails, I was able to fix the issue:
In IntelliJ 2019.02.04, Spring Boot configuration, select workspace $MODULE_WRK_DIR.
Controller:
#Controller
public class TestController {
#RequestMapping("/")
public String index() {
return "test.jsp";
}
}
Add this to application.properties:
spring.mvc.view.prefix=WEB-INF/
And put your jsp file in src/main/webapp/WEB-INF
if someone is still facing this issue on IntelliJ IDEA then just invalidate cache & restart the IDE. It will clear all downloaded dependencies & re-download all of them again. Hope this should solve your problem.

How is a war file created for spring boot with maven?

I'm trying to follow the guide for converting a spring project to a war.
http://spring.io/guides/gs/convert-jar-to-war/
It starts out using maven and gradle and then right after the jar portion it completely forgets about maven and only has gradle updates.
There are two main changes that you need to make in the pom. The first is to change the project's packaging type to war:
<groupId>org.springframework</groupId>
<artifactId>gs-convert-jar-to-war</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
The second is to add a dependency on spring-boot-starter-tomcat and mark it as provided:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
mvn package will now produce a war file that can be run using java -jar or deployed to a separate servlet container.
There is an official guide at spring:
http://spring.io/guides/gs/convert-jar-to-war-maven/
Pay attention to "Initialize the servlet" section.
It explains an important point of adding a class that substitutes web.xml. Without it (or without proper web.xml) you will get a war file but when deployed nothing will be accessible in browser as nothing will be registered as your request dispatcher.
Also note that it is best to run this example on Tomcat 8 as it supports latest servlet specs. I have spent number of hours trying to figure out why it does not work on my Tomcat 7.

Resources