thank you for your attention in reading my problem;
I'm trying to deploy a spring boot application but when I put it in the tomcat webapps folder it doesn't load my application, does anyone know what could be happening?
I already tried to run several versions of java changing the system variables, I used several versions of tomcat but none unzip the application
I'm using java version 17
and version apache-tomcat-9.0.71
Log Tomcat
Tomcat Past
I thought Spring Boot came with a pre-installed web server, so there is no need to use this method. Why don't you use the current method which supports auto deployment of your application?
An Spring Boot application is a ready to run jar file which contains tomcat/jetty and can be run with java -jar <your app>.jar.
When you want to run an application in tomcat you should build a .war file.
======================================
As found here: https://stackoverflow.com/a/27905557/2144466
Did you follow this guide: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto.traditional-deployment
and do you have a class which extends SpringBootServletInitializer andd overwrites the configure method:
`
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
`
Please follow below.
Rename your war file at location webapps from web-0.0.1-SNAPSHOT.war to web.war
restart tomcat.
open url (http://localhost:8080/web) in browser.
I managed to solve the problem.
Apparently it was version incompatibility between spring, tomcat and java, by adjusting the system variables I can deploy both java 11 and java 17 applications.
Even though I managed to find a solution, I thank the friends above for dedicating their time to trying to solve my problem.
I am developing a JEE application and I want to do some migration when the application starts. Actually, I am using payara-micro with Docker as a server. For an unknown reason, this code is never executed.
Here is my code :
#Singleton
#Startup
public class FlywayMigration {
#PostConstruct
public void startMigration(){
System.out.println("Starting flyway migration");
}
}
I added the javaee-api as dependency in my pom :
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
Here is the content of my Dockerfile :
FROM payara/micro
COPY ./target/mywebapp.war $DEPLOY_DIR
After building and running the docker image, the logs of the application shows that the app started well :
Payara Micro URLs
http://5b258e6a441a:8080/mywebapp
'mywebapp' REST Endpoints
GET /mywebapp/api/hello
GET /mywebapp/api/myresource
Can anyone help me find a solution ?
I finally make it work. The problem is that I was using the bad #Singleton annotation. Instead of using #javax.ejb.Singleton I used to #javax.inject.Singleton.
I wanted to integrate Spring boot with RestEasy.
I started using Paypal Springboot starter for the same by referring the link
I have added paypal resteasy dependency as mentioned in the above link.
But while deploying to Jboss server i am getting error below:
Caused by: java.lang.NoClassDefFoundError: org/jboss/resteasy/spi/NotImplementedYetException
at java.lang.Class.getDeclaredConstructors0(Native Method) [rt.jar:1.8.0_131]
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source) [rt.jar:1.8.0_131]
at java.lang.Class.getConstructor0(Unknown Source) [rt.jar:1.8.0_131]
at java.lang.Class.newInstance(Unknown Source) [rt.jar:1.8.0_131]
at org.jboss.as.web.deployment.ServletContainerInitializerDeploymentProcessor.loadSci(ServletContainerInitializerDeploymentProcessor.java:194)
at org.jboss.as.web.deployment.ServletContainerInitializerDeploymentProcessor.deploy(ServletContainerInitializerDeploymentProcessor.java:131)
Can someone please suggest me the best way to integrate Springboot with RestEasy?
These seem to be two separate questions here: how to integrate RESTEasy, and how to deploy to JBoss.
RESTEasy: The Paypal starter for RESTEasy that you mentioned has been transferred to a new home. https://github.com/resteasy/resteasy-spring-boot/blob/master/mds/USAGE.md
The latest versions works well. (I don't know what changed since the version you used.)
JBoss:
Converting a self-hosting Spring Boot application to one that runs on JBoss requires a few steps, and it's not clear from your writeup which of those changes you've already made.
1) In your pom.xml, change packaging from jar to war.
2) Also in pom.xml, exclude spring-boot-starter-tomcat from any dependencies that try to bring it in. Typically this is spring-boot-starter-web, but if you are using resteasy-spring-boot-starter you would exclude from that instead.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
3) Change main class and make it extend SpringBootServletInitializer.
4) Also in main override the configure method. (Some articles omit this step- it has to do with making sure your components are scanned correctly so there might be ways to configure a project so it is optional.)
public class NameOfMyMainClass extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(NameOfMyMainClass.class);
}
public static void main(String[] args) {
SpringApplication.run(NameOfMyMainClass.class, args);
}
}
These steps are written up in detail here: https://thepracticaldeveloper.com/2018/08/06/how-to-deploy-a-spring-boot-war-in-wildfly-jboss/
In theory that's all you need. But in practice, I never got it working (Spring Boot 2.0.4, JBoss 7.1.)
First issue: Spring Boot 2 apparently requires JBoss 7. I didn't learn this until after I'd wasted some time with JBoss 6.
Second issue: Even after upgrading the server, my JAX-RS beans never worked. According to the following article, JBoss 7.1.1 does not integrate cleanly and requires some additional workarounds: https://ilya-murzinov.github.io/articles/spring-boot-jboss/
There are articles that claim success, but note they tend to be using Wildfly, not JBoss. (It's also possible JBoss 7.0 didn't have all these problems.) So if you are following one of these articles, make sure they are using versions of Spring Boot and JBoss that you can use.
I have a simple Spring Boot application that gets messages from a JMS queue and saves some data to a log file, but does not need a web server. Is there any way of starting Spring Boot without the web server?
if you want to run Spring Boot 1.x without a servlet container, but with one on the classpath (e.g. for tests), use the following, as described in the spring boot documentation:
#Configuration
#EnableAutoConfiguration
public class MyClass {
public static void main(String[] args) throws JAXBException {
SpringApplication app = new SpringApplication(MyClass.class);
app.setWebEnvironment(false); //<<<<<<<<<
ConfigurableApplicationContext ctx = app.run(args);
}
}
also, I just stumbled across this property:
spring.main.web-environment=false
Spring Boot 2.x, 3.x
Application Properties
spring.main.web-application-type=NONE
# REACTIVE, SERVLET
or SpringApplicationBuilder
#SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}
Where WebApplicationType:
NONE - The application should not run as a web application and should not start an embedded web server.
REACTIVE - The application should run as a reactive web application and should start an embedded reactive web server.
SERVLET - The application should run as a servlet-based web application and should start an embedded servlet web server.
You can create something like this:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(false).run(args);
}
}
And
#Component
public class CommandLiner implements CommandLineRunner {
#Override
public void run(String... args) throws Exception {
// Put your logic here
}
}
The dependency is still there though but not used.
Spring boot will not include embedded tomcat if you don't have Tomcat dependencies on the classpath.
You can view this fact yourself at the class EmbeddedServletContainerAutoConfiguration whose source you can find here.
The meat of the code is the use of the #ConditionalOnClass annotation on the class EmbeddedTomcat
Also, for more information check out this and this guide and this part of the documentation
The simplest solution. in your application.properties file. add the following property as mentioned by a previous answer:
spring.main.web-environment=false
For version 2.0.0 of Spring boot starter, use the following property :
spring.main.web-application-type=none
For documentation on all properties use this link : https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
Use this code.
SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
For Spring boot v2.1.3.RELEASE, just add the follow properties into application.propertes:
spring.main.web-application-type=none
If you need web functionality in your application (like org.springframework.web.client.RestTemplate for REST calls) but you don't want to start a TOMCAT server, just exclude it in the POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Through program :
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(YourApplicationMain.class)
.web(WebApplicationType.NONE)
.run(args);
Through application.properties file :
spring.main.web-environment=false
Through application.yml file :
spring:
main:
web-environment:false
Spring boot has many starters, some starters have an embedded web server, some don't. The following have the embedded web server:
spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-starter-jetty
spring-boot-starter-tomcat
spring-boot-starter-jdbc
spring-boot-starter-data-rest
...
Pick the one that meets your requirements and that does not have server support.
I only need to make restful json api request in my spring application, so the starter I need is
spring-boot-starter-json
which provide RestTemplate and jackson for me to use.
You can use the spring-boot-starter dependency. This will not have the web stuff.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
If you want to use one of the "Getting Started" templates from spring.io site, but you don't need any of the servlet-related stuff that comes with the "default" ("gs/spring-boot") template, you can try the scheduling-tasks template (whose pom* contains spring-boot-starter etc) instead:
https://spring.io/guides/gs/scheduling-tasks/
That gives you Spring Boot, and the app runs as a standalone (no servlets or spring-webmvc etc are included in the pom). Which is what you wanted (though you may need to add some JMS-specific stuff, as someone else points out already).
[* I'm using Maven, but assume that a Gradle build will work similarly].
Remove folowing dependancy on your pom file will work for me
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
For Kotling here is what I used lately:
// src/main/com.blabla/ShellApplication.kt
/**
* Main entry point for the shell application.
*/
#SpringBootApplication
public class ShellApplication : CommandLineRunner {
companion object {
#JvmStatic
fun main(args: Array<String>) {
val application = SpringApplication(ShellApplication::class.java)
application.webApplicationType = WebApplicationType.NONE
application.run(*args);
}
}
override fun run(vararg args: String?) {}
}
// src/main/com.blabla/command/CustomCommand.kt
#ShellComponent
public class CustomCommand {
private val logger = KotlinLogging.logger {}
#ShellMethod("Import, create and update data from CSV")
public fun importCsv(#ShellOption() file: String) {
logger.info("Hi")
}
}
And everything boot normally ending up with a shell with my custom command available.
In Spring boot, Spring Web dependency provides an embedded Apache Tomcat web server. If you remove spring-boot-starter-web dependency in the pom.xml then it doesn't provide an embedded web server.
remove the following dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
From my experience on spring boot > 2.5 ,
if you plan to build the application as a jar file, in my opinion the solution of spring.main.web-application-type=NONE should not be widely accepted and used, since it has only a limited scope of benefits.
For asking to have spring boot without the web server, it means that you have from Spring either the dependency spring-boot-starter-web to build a spring web application or the dependency spring-boot-starter-jersey to build a jax-rs web application. Those dependencies pack inside the spring-boot-starter-tomcat which will then bring the dependency of tomcat-embed-core which is the actual tomcat server. This library is packed automatically inside and is of size ~3.3 MB. Even if you disable the server with the aforementioned property, you will still deliver your application jar file, containing the tomcat server inside.
So the con of just using the aforementioned property is that the deliverable jar file will be some MB larger in size without any actual need.
So if you want to have spring boot without the web server just don't use the dependencies spring-boot-starter-jersey or spring-boot-starter-web since if you build your application as a jar file there is no reason to have those dependencies and not have an embedded server delivered.
if you plan to build the application as a war file, you should also not use the above property.
In this case you will just need in your .pom the following configurations
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web or (spring-boot-starter-jersey)</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>pick the version that the server that already runs in production supports</version>
<scope>provided</scope>
</dependency>
</dependencies>
Exceptional cases
The property spring.main.web-application-type=NONE should be in my opinion used for some exceptional cases like if we build some web library that needs the above dependencies but is not to be used like a web application, or we have some complex type of testing that needs those libraries although the application does not need any server to run. This type of usages are however rare.
Similar to #nayun oh answer above, but for older versions of Spring, use this code:
SpringApplication application = new SpringApplication(DemoApplication.class);
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
application.run(args);
I have read a dozen tutorial on JaxWS and wsgen and none works ! Java really oever complicated for beginners compared to asp.net : in ASP.Net I can make a WS in 5 minutes not in Java : I'm stuck for several days now.
For example I followed this tutorial http://theopentutorials.com/examples/java-ee/jax-ws/create-and-consume-web-service-using-jax-ws/
I'm using Eclipse Luna, JDK 7 and Tomcat 7 and generated stubs and wsdl with wsgen.
I created this Endpoint Publisher class :
import javax.xml.ws.Endpoint;
import com.demo.JaxWS;
public class HelloEndpointPublisher {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Endpoint.publish("http://localhost:8080/EclipseJaxWSutotorial/JaxWS", new JaxWS());
}
}
When running it as Java Application why do I get this error and how to fix it ?
Exception in thread "main" java.util.ServiceConfigurationError: javax.xml.ws.spi.Provider: Provider com.sun.xml.ws.spi.ProviderImpl could not be instantiated
Update : I do not use maven. I have used wsgen by command line and it generated all classes, wsdl and xsd fine.
Have you check your library dependencies? Check if you have jaxws-rt in your library path. Basically you the maven dependencies will look like this
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.7</version>
</dependency>