java 如何将简单的 Spring Boot(使用 Gradle 构建系统)部署到 Apache Tomcat(真实服务器,而不是嵌入服务器)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35702975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deploy a simple Spring Boot (with Gradle build system) to Apache Tomcat (real server, not embed server)?
提问by Do Nhu Vy
I follow this tutorial: http://spring.io/guides/gs/rest-service/
This is my project structure:
build.gradle
我按照这个教程:http: //spring.io/guides/gs/rest-service/
这是我的项目结构:
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
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
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
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!)
我知道如何通过gradle bootRun
命令运行 Spring Boot 应用程序(在 Apache Tomcat 嵌入服务器上运行)。但我不知道如何在真正的 Apache Tomcat 上运行 Spring Boot 应用程序(此时请帮助我!)
回答by Do Nhu Vy
Follow comments's manish, I create source base from http://start.spring.io/
按照评论的 manish,我从http://start.spring.io/创建源库
Then I import to Eclipse for Java EE (with Spring Tools Suite, Gradle plugin), this is project folder structure:
然后我导入到 Eclipse for Java EE(使用 Spring Tools Suite,Gradle 插件),这是项目文件夹结构:
Greeting.java
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
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
我修改了文件 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
我不更改文件 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:
我可以在真正的 Tomcat 服务器上运行 Web 应用程序:
Then I use browser or Postman to view result:
然后我使用浏览器或邮递员查看结果:
http://localhost:8080/gs-rest-service/greeting?name=Vy
回答by azrael
You're using Gradle. Try to add this in your build.gradle file.
您正在使用 Gradle。尝试将其添加到您的 build.gradle 文件中。
dependencies {
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}
Don't forget to remove:
不要忘记删除:
compile("org.springframework.boot:spring-boot-starter-web")