4

I am new to Spring Boot, I have sample spring boot code in my Eclipse IDE.

Now to run the project. In project there is class "Application", I run that using Run As Java Application and then Its running on given port.

But I want it to run using Run on Server option of Eclipse, so whenever I try to run that on server it gives me 404.

Please give me any idea to resolve this issue.

Code:

@ComponentScan
@Configuration
@EnableAutoConfiguration
public class Application extends SpringBootServletInitializer {

@Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(Application.class);
        }

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);

        System.out.println("Let's inspect the beans provided by Spring Boot:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        System.out.println(Arrays.toString(beanNames));
    }
}

application.properties

server.address=localhost
server.port=8080
server.contextPath=/spring-security-cas
app.service.security=http://localhost:8080/j_spring_cas_security_check
app.service.home=http://localhost:8080/

pom.xml

<?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>org.esco.demo</groupId>
    <artifactId>spring-security-cas</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>demo-spring-security-cas</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>1.2.1.RELEASE</version>
        <relativePath />
    </parent>

    <dependencies>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-cas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
    </dependency>
    <!-- Usefull for accessing to jsp -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <!-- END Usefull for accessing to jsp -->
        <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.14.8</version>
    </dependency>
</dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>org.esco.demo.ssc.Application</start-class>
        <java.version>1.7</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Note: Project running when run Application as Run as Java Application, problem is I want it to run using Run on Server option.

Kamini
  • 690
  • 2
  • 13
  • 36
  • Please share your pom.xml,main class & application.properties – VelNaga Oct 12 '16 at 09:15
  • Deploy as a war instead of runnable jar. Also make use of "@SpringBootApplication" in main class then remove the following property "app.service.security" it should work. – VelNaga Oct 12 '16 at 09:23
  • maybe you mean how to add a project to a server https://stackoverflow.com/questions/15380125/cannot-add-a-project-to-a-tomcat-server-in-eclipse – Happy Oct 20 '20 at 11:24

2 Answers2

9

If you want to deploy it to a container, instead of using the embedded container follow this section in the reference guide.

In short the steps are

  1. Use war packaging instead of jar packaging
  2. Let your Application class extends SpringBootServletInitializer and implement the configure method.
  3. Mark the container dependencies (tomcat I guess) as provided

So in a nutshell

  1. Change <packaging>jar</packaging> to <packaging>war</packaging>
  2. Change your Application

    public class Application extends SpringBootServletInitializer {
        ...
        protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
            return builder.sources(Application.class);
        }
    }
    
  3. Add tomcat dependency as provided.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifcatId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    

For more elaborate information check the reference guide.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • I have followed above steps. – Kamini Oct 12 '16 at 09:42
  • And? Make sure eclipse is getting all dependencies, also check your logs. When changing packaging, you might need to reimport the project. Also make sure you have a servlet container that actually supports servlet 3.0. – M. Deinum Oct 12 '16 at 09:45
  • I delete project from eclipse and then import it , clicking on Run on Server I got error in my console. as below : Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration' – Kamini Oct 12 '16 at 09:56
  • Don't add code as comments... Update your question with it. When updating also add your dependencies. – M. Deinum Oct 12 '16 at 09:59
0

Do you mean a tomcat instance on eclipse? What are the logs saying? That there is no application deployed?

Then you need to deploy a war to tomcat -

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file

Change -

 <packaging>jar</packaging>

To

<packaging>war</packaging>

And it should work.

farrellmr
  • 1,815
  • 2
  • 15
  • 26
  • change the jar to war but the problems continues, I mean tomcat instance on eclipse, I am not getting any logs – Kamini Oct 12 '16 at 09:26
  • Isnt there a console output window on eclipse? Check Window > Show View> – farrellmr Oct 12 '16 at 09:27
  • Actully I am getting the server startup message in console " Server startup in 412 ms" – Kamini Oct 12 '16 at 09:31
  • Post the whole output to your question? – farrellmr Oct 12 '16 at 09:32
  • 1
    Have you actually read the documentation on that link? It also explains the use of `SpringBootServletInitializer`. Which you MUST use if you want a deployable artificat. – M. Deinum Oct 12 '16 at 09:32
  • @M.Deinum We havent confirmed the problem yet – farrellmr Oct 12 '16 at 09:35
  • @M.Deinum I changed my Application class and extend it by SpringBootServletInitializer. But still I have not got success. – Kamini Oct 12 '16 at 09:36
  • @M.Deinum Do I need to include web.xml? I mean without adding it I will not able to run it on server? Please correct me. – Kamini Oct 12 '16 at 09:39
  • Only extending it isn't going to do much, you still ahve to override a method and put something meaningful in there. – M. Deinum Oct 12 '16 at 09:39
  • @farrellmr His `Application` class isn't extending it, without that extends it will never run on a server. Unless you are on a very old server, use Spring Boot Legacy and provide a web.xml... – M. Deinum Oct 12 '16 at 09:39