Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy a simple Spring Boot (with Gradle build system) to Apache Tomcat (real server, not embed server)?

I follow this tutorial: http://spring.io/guides/gs/rest-service/
This is my project structure: enter image description here

build.gradle

buildscript{
    repositories{
        mavenCentral()
    }
    dependencies{
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar{
    baseName = 'gs-rest-service'
    version = '0.1.0'
}

repositories{
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies{
    compile("org.springframework.boot:spring-boot-starter-web") 
    testCompile("junit:junit")
}
task wrapper(type: Wrapper){
    gradleVersion = '2.3'
}



Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

}



Greeting.java

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        super();
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }

}



GreetingControler.java

package hello;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

}



I know how to run Spring Boot application by gradle bootRun command (run on Apache Tomcat embed server). But I don't know how to run above Spring Boot application on real Apache Tomcat (Please help me at this point!)

enter image description here

like image 283
Do Nhu Vy Avatar asked Feb 29 '16 15:02

Do Nhu Vy


People also ask

Is spring boot embedded Tomcat production ready?

Spring Boot aims to be production ready, by default. This means that it ships with useful defaults out of the box that may be overriden, if necessary. By default, Spring Boot provides an embedded Apache Tomcat build.


2 Answers

Follow comments's manish, I create source base from http://start.spring.io/

enter image description here

Then I import to Eclipse for Java EE (with Spring Tools Suite, Gradle plugin), this is project folder structure:

enter image description here

Greeting.java

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

GreetingController

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

I modified file GsRestServiceApplication.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class GsRestServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(GreetingController.class, args); // <-- modify this line.
    }
}

I don't change file GsRestServiceApplication.java

package hello;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}

I can run web app in real Tomcat server: enter image description here

Then I use browser or Postman to view result:

http://localhost:8080/gs-rest-service/greeting?name=Vy

enter image description here

like image 169
Do Nhu Vy Avatar answered Oct 01 '22 07:10

Do Nhu Vy


You're using Gradle. Try to add this in your build.gradle file.

dependencies {
   providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

Don't forget to remove:

compile("org.springframework.boot:spring-boot-starter-web") 
like image 24
azrael Avatar answered Oct 01 '22 05:10

azrael